1

I m trying to write a powershell script which will check the folder structure for a directory against a template folder structure layout and report back if its different i.e folders missing or different folders added.

Folder Template Structure
Folder A
Folder B

Directory 1 to check
Folder A
Folder B
Folder C

Directory 2 to check
Folder A

So for Directory 1 it would report Folder C is additional and for Directory 2 it would report Folder B is missing

Any help would be greatly appreciated

2 Answers 2

2
# Get the directories inside the template dir. as relative paths
$templateDirs = Get-ChildItem -Directory -Recurse -Name $templatePath

# Ditto for directory 1 and directory 2
$dir1Dirs = Get-ChildItem -Directory -Recurse -Name $dir1Path
$dir2Dirs = Get-ChildItem -Directory -Recurse -Name $dir2Path

# Compare to the template dirs.
Compare-Object $templateDirs $dir1Dirs
'---'  # Output separator string just to show distinct outputs.
Compare-Object $templateDirs $dir2Dirs

Note the use of -Name with Get-ChildItem, which causes all subdirectories (-Directory, -Recurse to be reported as paths relative to the input directory, which allows convenient comparison between directory trees.

Also note that the Compare-Object cmdlet by default outputs [pscustomobject] instances with two properties, and only for differences between the input sets:

  • .InputObject, in your case a relative directory path that is unique to one input set.
  • .SideIndicator, which is a string indicating whether the input object was unique to the left side (the first input set, implicitly bound to parameter -ReferenceObject) - '<=' - or to the right side (the second input set, implicitly bound to parameter -DifferenceObject) - '=>'

The above yields something like:

InputObject SideIndicator
----------- -------------
C           =>             # folder C only in dir. 1, not in template dir.
---
B           <=             # folder B only in template dir., not in dir. 2
Sign up to request clarification or add additional context in comments.

Comments

0

To compare two folders, perform the following steps (from: https://blogs.technet.microsoft.com/heyscriptingguy/2011/10/08/easily-compare-two-folders-by-using-powershell/ ):

  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.

Code:

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

$fsoBU = Get-ChildItem -Recurse -path C:\template

Compare-Object -ReferenceObject $fso -DifferenceObject $fsoBU

1 Comment

This won't work robustly, because Compare-Object compares the System.IO.DirectoryInfo objects by their .ToString() value, which in Windows PowerShell is the mere filename in this case. To truly compare two folder trees, you must compare their items by their relative paths within the subtree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.