Although there is software that will sync files and folders for you, even paid software can be limiting. A good PowerShell script can help.
Copy-Item -Path C:Folder* -Destination SERVER2c$Folder2is
There are a few ways to handle syncing. You can purchase software or even download free software, which will probably work well, but sometimes it might limit you in what you can achieve. This is why using a PowerShell script to accomplish syncing is the best option. By using PowerShell, you’re not boxed in by off the shelf software. You can generally implement any feature you like as long as you have enough experience and time. This is what we’re going to be covering today.
Syncing files with PowerShell
First, as with everything in scripting, there are numerous ways to make this happen. Some methods have features like automatic triggers, file hashing and other advanced features but these methods would take much too long to go over in a short article. If you’re an advanced PowerShell scripter I recommend checking out Steven Murawski’s work on the Sync framework.
For today, we’ll just be covering the basics. We’ll be manually kicking off the script rather than using automatic triggers and we’ll be using basic PowerShell commands — no complicated .NET objects to speak of. However, we’ll still come out with a useful way you can sync files from one folder to another.
The first thing we’ll need to do is define the locations of each folder.
$Folder1Path = ‘C:Folder1’
$Folder2Path = ‘C:Folder2’
For demonstration purposes, we created these two folders and have placed 101 identical text files into each folder.
Next, we’ll have to discover everything in each of these folders. This will give us two collections to compare.
$Folder1Files = Get-ChildItem -Path $Folder1Path
$Folder2Files = Get-ChildItem -Path $Folder2Path
Now, we’ll need to compare them. A great PowerShell cmdlet to do this is Compare-Object. Compare-Object 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. Since both of our folders are in sync at the moment, this is what we see. However, what happens when we add a file to one of the folders?
In the example, we created a dummy file in $Folder1 and now you can see the folders are out of sync. Compare-Object shows that newfile1234.xml is in $Folder1 but not in $Folder2. We now need to copy whatever files Compare-Object finds to the folder that doesn’t contain the file. To do this, we’ll have to create a script like the following.
You’ll see that we’re getting the output of Compare-Object that contains all the different files. We’re then reading each of those files into foreach loop and checking to see which sides needs to copy from and to. You’ll see that this works well but we’re not done.
Removing files with PowerShell
What about file removals? Let’s say a file is removed from one of the folders and you’d like to remove the file in the other folder rather than copy the new one over?
In this example, we removed a file from $Folder2 and you can see that Compare-Object can tell this. Now we’d like to remove that same file from $Folder1.
To do that, we simply have to remove all the files that Compare-Object finds.
You’ll see that the file is gone. Using this kind of approach with PowerShell will allow you to keep both folders in sync.
However, we haven’t accounted for the complexities of removing and copying at the same time and subfolders as well. If you’d like more in-depth information about syncing folders, we recommend checking out Steven Murawski’s blog post mentioned earlier or this blog post. Both offer much more advanced approaches to syncing folders with PowerShell.