Compare-Object: Find item in file 1 that is not in file2

The objective is to compare two files, one reference list the other is the testing list. My colleague helped me wrote a script to do the comparison, there were many false positives, so here’s his script:

$output = Compare-Object $file1 $file2 -Property ip -IncludeEqual | Where-Object {$_.SideIndicator -eq "<="}
$output | select ip | Export-Csv .\logs\xxx_ip_diff.csv  -NoTypeInformation

The output always give me items i have on both files and some items from file1.

From stackoverflow i have found a solution to this problem.
See here

So based on the solution, I modified the commands like this:

function find_discrepancy($devices) {
    foreach($device in $devices.Keys) {
        $diff = (Import-Csv "C:\Temp\${device}_ip.csv").IP
        Compare-Object $base $diff -IncludeEqual | Where-Object { $_.SideIndicator -eq '<=' } | ForEach-Object { $_.InputObject } | Set-Content "C:\temp\diff_${device}_ip.csv"
        
        }
        
    }

So actually to properly enumerate the objects that has "<=" I need to use the command foreach-object.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s