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.
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:
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.
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.
If you’re relatively new to PowerShell, consider trying out commands in a test folder first to avoid accidental data loss.
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.
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
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.
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:
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.
You can list items in a directory using the Get-ChildItem cmdlet in PowerShell, making it easier to track your folder’s contents.
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 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:
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.