PS: Compare Files

1 minute read

Description:

Many administrators create baselines for systems when they are working and then take snapshots afterwards to compare. Follow these steps to compare files in PS.

To Resolve:

  1. Let’s use Processes for example. We will compare current processes with a “baseline” test (a snapshot of when the system was stable).
Get-Process | Export-Clixml C:\Procs.Xml
  1. Open a few “notepad.exe” instances. Now run:
Compare-Object (Import-Clixml C:\Procs.Xml) | (Get-Process)

 

  1. Type:
Compare-Object (Import-Clixml C:\Procs.Xml) (Get-Process) -Property Name
  1. Use the above steps to compare just about anything in Powershell. CliXML is preferred because it holds the most properties similar to an object.

NOTE: Use the second reference to learn more about Compare-Object


Another example:

# Shows same between 1 and 2
# This shows all the servers that are the same between the vuln.txt and prod.txt.
C

ompare-Object -ReferenceObject $( Get-Content "c:\_gwill\temp\vuln.txt" ) -DifferenceObject $( Get-Content "c:\_gwill\temp\prod.txt" ) -IncludeEqual -ExcludeDifferent

# Shows what is in 2 that is not in 1.
# Shows all the servers that are in prod.txt, but not in vuln.txt

Compare-Object -ReferenceObject $( Get-Content "c:\_gwill\temp\vuln.txt" ) -DifferenceObject $( Get-Content "c:\_gwill\temp\prod.txt" ) |
Where-Object -Property SideIndicator -eq '=>'

# Shows what is in 1 that is not in two
# Shows all the servers that are in vuln.txt, but not in prod.txt

Compare-Object -ReferenceObject $( Get-Content "c:\_gwill\temp\vuln.txt" ) -DifferenceObject $( Get-Content "c:\_gwill\temp\prod.txt" ) |
Where-Object -Property SideIndicator -eq '<='

References:

http://windowsitpro.com/blog/creating-and-comparing-configuration-baselines
http://powershell.com/cs/blogs/tobias/archive/2009/01/09/tipps-amp-tricks-using-compare-object.aspx