3.10. Detecting Changes and Differences Among Objects

All scripting, sooner or later, will hit the task of comparing two objects or collections, trying to find what has been added or deleted. Compare-Object is our friend here: it knows how to loop over two objects, the reference object and another one, and report on their differences. Traditional diff tools work on text only, while Compare-Object works on real objects. Here is how we can use it to detect the changes between two folders: the current one and yesterday's backup:

PS> diff (dir PowerShellBackup) (dir PowerShell)

InputObject                           SideIndicator
-----------                           -------------
kill.log                              =>

The => side indicator means that the kill.log object is present in the right-hand collection, the difference ...

Get Pro Windows PowerShell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.