Search code examples
powershelldirectorycompareobject

Powershell script to compare folder structure


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


Solution

  • # 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