BDC Hamburger Icon

MENU

Close
BDC Logo
Search Icon
Advertising Disclosure
Close
Advertising Disclosure

Business.com aims to help business owners make informed decisions to support and grow their companies. We research and recommend products and services suitable for various business types, investing thousands of hours each year in this process.

As a business, we need to generate revenue to sustain our content. We have financial relationships with some companies we cover, earning commissions when readers purchase from our partners or share information about their needs. These relationships do not dictate our advice and recommendations. Our editorial team independently evaluates and recommends products and services based on their research and expertise. Learn more about our process and partners here.

Updated Jul 06, 2023

How To Sync Folders With PowerShell

While software can sync files and folders for you, PowerShell can help you build a custom solution.

author image
Written By: Adam BertramSenior Writer
Verified CheckEditor Verified:
Verified Check
Editor Verified
Close
A business.com editor verified this analysis to ensure it meets our standards for accuracy, expertise and integrity.
Gretchen Grunburg
Senior Editor
Business.com earns commissions from some listed providers. Editorial Guidelines.
Table Of Contents Icon

Table of Contents

Open row

Although specific software can sync files and folders for you, paid software might not offer the flexibility you need. However, a good PowerShell script can help you build a custom solution that meets all of your requirements. 

We’ll explain what you need to know about syncing files and folders with Microsoft PowerShell.

Why would you need to sync folders?

Copying files from one folder to another is straightforward: Drag them across your window, or use a simple command like this:

Copy-Item -Path C:Folder* -Destination SERVER2c$Folder2 

But what if you want to ensure a complete replica of a folder instead of just copying files from one folder to another? This is when you must sync the two folders. 

Essentially, syncing is just copying files, but it’s smarter. Syncing involves the following:

  • Reading both folders’ entire contents
  • Calculating the differences
  • Deciding which files to copy in what direction

Software syncing solutions can work, but they may not allow you to do everything you want. This is why using a PowerShell script to accomplish syncing is the best option. 

FYIDid you know
Learning to sync files in PowerShell is excellent training that provides the skills you'll need for more advanced functions, like file hashing, automatic triggers and web scraping.

Syncing files with PowerShell

PowerShell is more flexible than off-the-shelf software. You can get it to perform whatever functions you want. Better still, you can make it easy for colleagues to use by creating a customized interactive menu with point-and-click syncing. 

As with everything in scripting, there are numerous ways to sync files with PowerShell. Some methods incorporate advanced features such as automatic triggers and file hashing. However, we’ll look at the more straightforward method of using basic PowerShell commands instead of complicated .NET objects. We’ll also manually kick off the script instead of using automatic triggers. 

TipBottom line
If you’re relatively new to PowerShell, consider trying out commands in a test folder first to avoid accidental data loss.

1. Define the folders’ locations.

Just as you would with websites and other URLs when managing IIS application pools, the first thing you must do when syncing files between folders is define each folder’s location. Here’s how:

$Folder1Path = ‘C:Folder1’
$Folder2Path = ‘C:Folder2’

For demonstration purposes, create these two folders and put 101 identical text files into each folder.

PowerShell sync folder

2. Discover what’s in the folders. 

Next, you must discover everything in each folder. This will give you two collections to compare. Here’s how:

$Folder1Files = Get-ChildItem -Path $Folder1Path
$Folder2Files = Get-ChildItem -Path $Folder2Path

3. Compare the folders’ contents. 

Now, you must compare the contents of both folders. A great PowerShell cmdlet to do this is Compare-Object, which allows you to specify two different collections. It will then look at each one and output which items are in the difference collection and which items in the reference collection are different.

Compare-Object -ReferenceObject $Folder1Files -DifferenceObject $Folder2Files

If each collection is the same, it will output nothing. However, when you add a file to one of the folders, you’ll see that the folders are out of sync.

PowerShell sync folder examples

In that example, you created a dummy file in $Folder1, making the folders out of sync. Compare-Object shows that newfile1234.xml is in $Folder1 but not in $Folder2. 

Now, you must copy whatever files Compare-Object finds to the folder that doesn’t contain the file. This is similar to how you would manage file system ACLs with PowerShell to identify and address what’s missing. 

To sync the folders, you must create a script like this:

PowerShell sync folder

The output of Compare-Object will now contain all of the different files. You’re then reading each file into a for-each loop and checking to see which side must copy which files. 

This works well — but you’re not done yet.

Did You Know?Did you know
You can list items in a directory using the Get-ChildItem cmdlet in PowerShell, making it easier to track your folder’s contents.

Removing files with PowerShell

You may need to remove files. Let’s say a file is removed from one of the folders and you’d like to remove the file in the other folder instead of copying over the new file.

In this example, we’ll remove a file from $Folder2, and you can see that Compare-Object can spot the file to be removed. 

PowerShell sync folder code

Did You Know?Did you know
PowerShell helps you maintain accuracy and consistency across your data by creating customizable scripts that manage and sync data between folders.

Next, you must remove the file (or files) that Compare-Object finds:

PowerShell coding

The file will be removed. 

Syncing folders is like managing user profiles in PowerShell. In both cases, you ensure consistency through automation. With user profiles, you make sure that each user has the same settings; with syncing folders, you ensure that each folder has the same files.

Mark Fairlie contributed to this article.

Did you find this content helpful?
Verified CheckThank you for your feedback!
author image
Written By: Adam BertramSenior Writer
Adam Bertram is an IT expert and business owner who has spent decades advising on network administration and security, designing and building infrastructure, and creating and teaching courses on Windows Server, Powershell and more. While maintaining his own IT business, he has provided hands-on DevsOps services for clients like JPMorgan Chase. At business.com, Adam covers the ins and outs of PowerShell, helping companies improve their Windows configurations and automations. Bertram, who has a degree in computer science, holds Microsoft, Cisco and CompTIA credentials. He has written numerous tutorials, guides and books, including "Building Better PowerShell Code: Applying Proven Practices One Tip at a Time."
BDC Logo

Get Weekly 5-Minute Business Advice

B. newsletter is your digest of bite-sized news, thought & brand leadership, and entertainment. All in one email.

Back to top