Categories: Blog

Updated WintellectPowerShell for October 14, 2012

Recently I had to compare a bunch of directory pairs and while PowerShell offers the Compare-Object, I needed something more that would not only look at filenames but compare binary files as well. Figuring some nice person on the Internet had already implemented this, I thought I’d just borrow and go about my business. Turns out there were lots of people asking about comparing directories, but not combining both filename and binary comparisons. Other than a mercifully brief divergence into PowerShell custom objects because I always wondered what they were, it wasn’t too hard to implement Compare-Directories.

Below is the help for Compare-Directories that shows how it works. As always, you can get the latest WintellectPowerShell module at it’s happy home on GitHub: https://github.com/Wintellect/WintellectPowerShell. If you follow the project on GitHub you’ll get these updates automatically.

NAME
    Compare-Directories
    
SYNOPSIS
    Compare two directories to see if they are identical
    
SYNTAX
    Compare-Directories [-OriginalDir] <String> [-NewDir] <String> [[-Excludes] <String[]>] [-Recurse] [-Force]
    [-Content] [<CommonParameters>]
    
DESCRIPTION
    This cmdlet will compare two directories and report if the files are identical by name, and optionally on content.
    
    Symbol explanation:
    => – The file is in the -NewDir directory, not the -OriginalDir.
    <= – The file is in the -OriginalDir directory and not the -NewDir.
    != – The file is in both directories, but the content is not identical.
    
    If the directories are identical an empty hash table is returned.
    
    Since sometimes filenames are long, you can pipe this output of this cmdlet into Format-Table -AutoSize to avoid
    truncating the filenames.    

PARAMETERS
    -OriginalDir <String>
        The original directory to use for the comparison.
        
    -NewDir <String>
        The new directory to compare to.
        
    -Excludes <String[]>
        The array of exclusions, including wildcards, so you can filter out some of the extraneous files.
        
    -Recurse
        Recurse the directory tree. The default is to just look at the directory.
        
    -Force
        Allows the cmdlet to get items that cannot otherwise not be accessed by the user, such as hidden or system
        files.
        
    -Content
        Check the content of matching filenames in both directories to see if they are equal. This is done through the
        Get-Hash cmdlet also in this module.
            
OUTPUTS
    HashTable
        
        The name is the file, and the value is the difference indicator. If the directories are identical, an empty
        hash table is returned.
    
    ————–  Example 1 ————–
    
    C:\PS>Compare-Directories .\Original .\Copied -Content
    
    Compares the original directory against a copied directory for both filenames and content.
    
    This shows that both file a.pptx, and c.pptx are in both directories but the content is different. Files f.pptx
    and i.pptx are only in the .\Copied directory.
    
    Name                           Value
    —-                           —–
    a.pptx                         !=
    c.pptx                         !=
    f.pptx                         =>
    i.pptx                         =>

John Robbins

Recent Posts

How to Navigate Azure Governance

 Cloud management is difficult to do manually, especially if you work with multiple cloud…

5 days ago

Why Azure’s Scalability is Your Key to Business Growth & Efficiency

Azure’s scalable infrastructure is often cited as one of the primary reasons why it's the…

3 weeks ago

Unlocking the Power of AI in your Software Development Life Cycle (SDLC)

https://www.youtube.com/watch?v=wDzCN0d8SeA Watch our "Unlocking the Power of AI in your Software Development Life Cycle (SDLC)"…

1 month ago

The Role of FinOps in Accelerating Business Innovation

FinOps is a strategic approach to managing cloud costs. It combines financial management best practices…

1 month ago

Azure Kubernetes Security Best Practices

Using Kubernetes with Azure combines the power of Kubernetes container orchestration and the cloud capabilities…

1 month ago

Mastering Compliance: The Definitive Guide to Managed Compliance Services

In the intricate landscape of modern business, compliance is both a cornerstone of operational integrity…

2 months ago