How it works...

Filtration of the output is very simple in PowerShell. Given that the content output is an object, we could simply use the properties from within the object for the filtration.

This recipe shows two modes of filtration:

  1. Using one property and comparing its value to the input,
  2. Using a filter script, that uses multiple values, and multiple conditions.

The Property parameter accepts only one property. A conditional operator and a value for comparison are added to the statement to filter the output.

The FilterScript parameter, on the other hand, can handle more complex filtration, for example, when we need the output to meet several conditions.

A significant difference between using Property and FilterScript is the use of the automatic variable, $_. This variable contains the current object in the pipeline. For example, in this recipe, we pass the object from Get-ChildItem to Where-Object through the pipeline. The automatic variable, $_, contains the object returned by Get-ChildItem, so that Where-Object can process it. $_.LastWriteTime, in this case, picks the LastWriteTime property from the object returned by Get-ChildItem.

Also, the LastWriteTime object is of type System.DateTime or DateTime. (Run (Get-ChildItem .).LastWriteTime | Get-Member to know more.) Therefore, it is possible to break it down further into days, hours, minutes, and so on, which is the reason, $_.LastWriteTime.Minute could be used for filtration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.41.187