1. Powershell Compare Files In Directories
  2. Get Files In Directory Powershell
  3. Compare Files In Directory

Summary: Microsoft Scripting Guy Ed Wilson illustrates how to compare two folders by using Windows PowerShell.

Files and directories are compared both on filename and contents using a MD5hash. Internally, Compare-Object is used to compare the directories. The behavior and results of Compare-Directory is similar to Compare-Object. Feb 03, 2014  Listing files in folders and subfolders with PowerShell When it comes to working with files and folders and recursing through a nested directory structure, it is almost an unfair competition between Windows PowerShell and VBScript. It is almost like the Windows PowerShell team deliberately made it easy to work with files and folders. Compare Folder Names to File Names, Create Arary, Feed Command, Loop 5 Times. The MPEG2 movies are in folders and are not files. The script would need to read the folder names and compare it to mpeg4 file names. So the MP2 movie folder 'movies' has sub folders, movie1, movie2, movie3, and movie4. The topic ‘Compare Folder Names to File.

Microsoft Scripting Guy Ed Wilson here. It is an absolutely beautiful day in Charlotte, North Carolina. The early morning rain gave way to a colorful rainbow.

My manager bought me a new laptop, and I have been busy working on it; installing software, copying files, and migrating settings. The old laptop will be paved, so it is important to ensure I get everything copied to the new laptop.

Most of the major things are automatically transferred, but there are always a few folders that seem to get left behind. This is especially true because I am still working while doing the migration, and there is always a danger of missing something.

PowerShell How-To. How To Compare the Contents of Two Folders with PowerShell. Make sure two documents are both exact and in sync with this process. One way I can do this is just ensuring the files in each folder are all Word documents and have the same number of them in each folder.

In the past, I might write a script to compare two folders to ensure they are identical. However, with Windows PowerShell, I do not need to write a script. I can type a simple command to compare two folders.

For example, I use a folder named fsothat is located directly off of the root of the C:drive as my scratch directory. I leave all kinds of stuff in that directory, including files that contains important sample scripts and sample text files, spreadsheets, and databases. I use these files when writing articles, teaching, or making presentations. This folder is shown in the following figure.

There is nothing vital in the folder, but it is useful to have those files, so I want to ensure I have a good copy of the folder. Also, I am not capable of quickly reading through a folder with 144 files to ensure nothing is missing. To compare two folders I perform the following steps:

  1. Use the Get-ChildItem cmdlet with the recurse switched parameter and the path parameter (points to the folder to use for reference) to obtain a collection of fileinfo objects. Store these objects in a variable.
  2. Use the Get-ChildItem cmdlet with the recurse switched parameter and the path parameter (points to the folder to use for comparison) to obtain a collection of fileinfo objects. Store these objects in a different variable.
  3. Use the Compare-Object cmdlet and specify the objects stored in the first variable to the ReferenceObject parameter. Supply the objects stored in the second variable to the DifferenceObject parameter.

Note Do not get hung up on whether the first folder should be the reference object or the difference object. The position of the folder in the two parameters determines the direction of the comparison arrows, but as long as you know which folder is difference or reference, you will be fine.

The code I type on my laptop is shown here:

$fso = Get-ChildItem -Recurse -path C:fso

$fsoBU = Get-ChildItem -Recurse -path C:fso_BackUp

Compare-Object -ReferenceObject $fso -DifferenceObject $fsoBU

The code and associated output are shown in the following figure. The output tells me that inputobject(this is the difference object parameter) is missing three files: a.txt, b.txt, and c.txt. I need to copy these three files to the c:fso_backup folder.

Well, it is the weekend, and as you can see, it is a beautiful day. I am going to get back to work on my laptop. Hope you have an awesome weekend. See you tomorrow.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Active11 months ago

PowerShell noob here.

I have two different folders with xml files. One folder (folder2) contains updated and new xml files compared to the other (folder1). I need to know which files in folder2 are new/updated compared to folder1 and copy them to a third folder (folder3). What's the best way to accomplish this in PowerShell?

Opentext brava dwg viewer. We hand pick programs that we know can open or otherwise handle each specific type of file. It helps Windows select the right program to open the file. • Original downloads only All software listed on file.org is hosted and delivered directly by the manufacturers. • We help you open your file We have a huge database of file extensions (file types) with detailed descriptions.

KeithPowershell Compare Files In DirectoryKeith
8537 gold badges24 silver badges44 bronze badges

6 Answers

OK, I'm not going to code the whole thing for you (what's the fun in that?) but I'll get you started.

First, there are two ways to do the content comparison. The lazy/mostly right way, which is comparing the length of the files; and the accurate but more involved way, which is comparing a hash of the contents of each file.

For simplicity sake, let's do the easy way and compare file size.

Basically, you want two objects that represent the source and target folders:

Then you can use Compare-Object to see which items are different..

Compare-Object $Folder1 $Folder2 -Property Name, Length

which will list for you everything that is different by comparing only name and length of the file objects in each collection.

You can pipe that to a Where-Object filter to pick stuff that is different on the left side..

Compare-Object $Folder1 $Folder2 -Property Name, Length Where-Object {$_.SideIndicator -eq '<='}

And then pipe that to a ForEach-Object to copy where you want:

JNKJNK
52.4k12 gold badges104 silver badges125 bronze badges

Recursive Directory Diff Using MD5 Hashing (Compares Content)

Here is a pure PowerShell v3+ recursive file diff (no dependencies) that calculates MD5 hash for each directories file contents (left/right). Can optionally export CSV's along with a summary text file. Default outputs results to stdout. Can either drop the rdiff.ps1 file into your path or copy the contents into your script.

USAGE: rdiff path/to/left,path/to/right [-s path/to/summary/dir]

Here is the gist. Recommended to use version from gist as it may have additional features over time. Feel free to send pull requests.

cchamberlaincchamberlain
10.6k5 gold badges46 silver badges60 bronze badges

Further to @JNK's answer, you might want to ensure that you are always working with files rather than the less-intuitive output from Compare-Object. You just need to use the -PassThru switch..

This at least means you don't have to worry about which way the SideIndicator arrow points!

Also, bear in mind that you might want to compare on LastWriteTime as well.

Looping through the sub-folders recursively is a little more complicated as you probably will need to strip off the respective root folder paths from the FullName field before comparing lists.

You could do this by adding a new ScriptProperty to your Folder1 and Folder2 lists:

You should then be able to use RelativePath as a property when comparing the two objects and also use that to join on to 'C:Folder3' when copying to keep the folder structure in place.

Charlie JoyntCharlie Joynt

Do this:

And even recursively:

Powershell Compare Files In Directories

and is even hard to forget :)

Francesco MantovaniFrancesco Mantovani

Simple file-level comparasion

Call it like PS > .DirDiff.ps1 -a .Old -b .New

Possible output:

Bruno ZellBruno Zell
2,1841 gold badge17 silver badges28 bronze badges
Files

gci -path 'C:Folder' -recurse where{$_.PSIsContainer}

-recurse will explore all subtrees below the root path given and the .PSIsContainer property is the one you want to test for to grab all folders only. You can use where{!$_.PSIsContainer} for just files.

Get Files In Directory Powershell

ChrisChris

Compare Files In Directory

Not the answer you're looking for? Browse other questions tagged filepowershellcomparedirectory or ask your own question.