business.com receives compensation from some of the companies listed on this page. Advertising Disclosure
World's Best Boss

Do you have the world's best boss?Enter them to win two tickets to Sandals!

BDC Hamburger Icon

MENU

Close
BDC Logo
Search Icon
Updated Jul 06, 2023

How To Sync Folders With PowerShell

author image
Adam Bertram, Contributing Writer

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.

author image
Adam Bertram, Contributing Writer
Adam Bertram is a 20-year veteran of IT and experienced online business professional. He's an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn, or follow him on Twitter at @adbertram or the TechSnips Twitter account at @techsnips_io.
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