Your free business.com+ membership unlocks exclusive tech deals and advisor support
Join Free
BDC Hamburger Icon

Menu

Close
BDC Logo with Name
Search Icon
Search Icon
Advertise with us
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.

How to Sync Folders With PowerShell

While commercial software can handle basic file synchronization, a custom PowerShell script gives businesses more control over complex workflows.

author image
Written by: Adam Bertram, Senior WriterUpdated Feb 23, 2026
Gretchen Grunburg,Senior Editor
Business.com earns commissions from some listed providers. Editorial Guidelines.
Table Of Contents Icon

Table of Contents

Open row

You can buy dedicated synchronization tools, but they don’t always offer the level of flexibility some teams need. A well-built PowerShell script lets you create a setup that fits your exact environment instead of forcing your processes into a preset tool. In this guide, we’ll walk through how to sync files and folders — and remove outdated ones — using Microsoft PowerShell.

Why would you need to sync folders?

Moving files between directories is usually a simple task. You might drag and drop icons or execute a basic command like the one below:

Copy-Item -Path C:\Folder* -Destination \SERVER2\c$\Folder2

However, copying is not the same as synchronizing. If you need a mirror image of a folder — where the destination automatically updates to match the source — you need a sync process. Syncing works more like an intelligent copy process because it analyzes the state of your data to improve efficiency.

Syncing involves the following:

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

Off-the-shelf tools don’t always provide the level of logic or scheduling some IT teams require. In these cases, scripting your own synchronization tool can be a more flexible approach.

Joey D’Antoni, a Microsoft Data Platform and Azure MVP and senior Azure infrastructure architect, is a strong proponent of using PowerShell for data-driven tasks. “PowerShell is a really powerful tool set,” D’Antoni said. “It can be used to do just about anything in the Windows, Azure, or even Linux/MacOS ecosystem.”

D’Antoni noted that automating file transfers is an excellent entry point for learning the language. “Figure out the PowerShell commands to accomplish this — write error handling and build looping and logging structures,” he said. “Then you are way ahead with PowerShell.”

FYIDid you know
Learning to sync files in PowerShell can also serve as practical training for more advanced functions, such as building a web scraping tool in PowerShell.

How to sync files with PowerShell

PowerShell offers a high level of flexibility compared with many off-the-shelf applications. You can program it to execute logic that fits your specific infrastructure. You can also make scripts easier for teammates to use by building an interactive menu inside PowerShell, including simple point-and-click options for syncing tasks.

There are multiple approaches to file synchronization in PowerShell. Advanced users might utilize file hashing or event triggers, but for this guide, we focus on a practical method using built-in cmdlets. We’ll also run the script manually rather than setting up automated triggers.

Sync Files Steps:Did you know
  1. Define the locations of each folder.
  2. Retrieve a list of all files in each folder.
  3. Compare the contents of both folders.

1. Define the folders’ locations.

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

$Folder1Path = ‘C:\Folder1’

$Folder2Path = ‘C:\Folder2’

If you want to follow along, create two test folders on your local drive named “Folder1” and “Folder2,” then add a few identical text files to each location.

PowerShell script defining folder paths in the Integrated Scripting Environment

2. Discover what’s in the folders.

Start by generating an inventory of both directories so you can identify any discrepancies. The Get-ChildItem cmdlet retrieves file objects from each path.

$Folder1Files = Get-ChildItem -Path $Folder1Path

$Folder2Files = Get-ChildItem -Path $Folder2Path

These commands capture the contents of your folders into variables. The script can now analyze the file lists stored in $Folder1Files and $Folder2Files.

3. Compare the folders’ contents.

At the center of this process is the Compare-Object cmdlet. This utility analyzes two sets of objects and identifies which items exist in one location but not the other.

Compare-Object -ReferenceObject $Folder1Files -DifferenceObject $Folder2Files

If the folders match, the cmdlet returns no results. However, if there is a discrepancy — such as a new file in one folder — the output highlights the difference. Microsoft documentation notes that Compare-Object is commonly used to evaluate differences between object collections in PowerShell.

PowerShell Compare-Object output showing differences between two folders

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

Once the difference is identified, you can program the script to copy the missing file to the destination folder. That’s 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 script using a ForEach loop to copy missing files

The script captures the output of Compare-Object, loops through each difference and copies the specific files required to make the folders match. It 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 folders’ contents.

How to remove files with PowerShell

Fully synchronizing folders involves more than adding new files; it can also mean removing outdated ones. If a file is deleted from the source, it should often be removed from the backup location to maintain an accurate mirror.

In the following example, we assume a file was deleted from the source folder ($Folder1). We use Compare-Object to identify this “orphan” file in $Folder2.

PowerShell code snippet showing how to identify a file that needs removal

Once identified, you can pipe the object into a removal command to delete it from the destination folder.

Final PowerShell script showing file removal logic

The target file is removed, and your folders are back in sync.

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

D’Antoni emphasized the importance of testing when rolling out changes. “Automation is awesome, but very powerful,” he cautioned. “If you can change something on thousands of servers at once, that’s awesome. But you can also break thousands of servers very quickly. Pro tip: PowerShell has a -whatif flag to test your code without actually running it. Even after that, always test on a subset of systems.”

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

Expert advice on PowerShell

Many IT teams use PowerShell to manage complex cloud environments and data workflows, but the same core ideas apply to everyday file management tasks. According to D’Antoni, building these foundational skills can help you grow into more advanced automation work. Consider the following advice:

  • Use PowerShell modules to streamline operations. D’Antoni highlighted the importance of using PowerShell modules to streamline operations. “As you build a collection of PowerShell scripts for your usage, using modules allows you to include helper functions — reusable utilities that simplify larger workflows or common tasks,” he said. “Modules also make it easier to deploy code to other computers by bundling related scripts together, improving efficiency and consistency across your environments.”
  • PowerShell is a robust automation tool. D’Antoni shared insights from his time at Comcast, where he served as principal architect for the firm’s SQL Server solutions. “At Comcast, I worked with a team to build a private cloud,” he recalled. “PowerShell drove all of our automation in the Windows space — all of the pre- and post-deployment configuration. That was a lot of dev work, but another good example is the dbatools module, an open-source module that helps administrators automate complex SQL Server database-related tasks.” D’Antoni also emphasized PowerShell’s versatility. “It was also really effective for managing smaller, essential tasks like syncing folders for greater consistency and efficiency.”
  • Just start coding. D’Antoni advised PowerShell beginners to dive in. “Just start writing code,” he said. “Find your problem and start with it. Learn PowerShell in a Month of Lunches is an excellent book that you can start learning with.”

Mark Fairlie contributed to this article. Source interviews were conducted for a previous version of this article.

Did you find this content helpful?
Verified CheckThank you for your feedback!
author image
Written by: Adam Bertram, Senior 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."