PowerShell provides useful cmdlets those can be used with other cmdlets to select the objects, sort the results etc,. These are core cmdlets and when we develop our own cmdlet, we no need to focus on sorting the results as we can use already provided cmdlet to sort the objects. This gives the freedom to focus on actual cmdlet functionality.
In this Article, we will go through Where-Object
cmdlet.
Where-Object
cmdlet
Where-Object
cmdlet is useful to select objects based on the given condition or criteria. You can decide what data to be displayed as a result using this cmdlet.
This cmdlet need objects or collection as an input. Hence we always have to use this with other cmdlets. The result of the other cmdlet is an input to this cmdlet.
Let us take a simple example. When we run Get-Command
we can see a lengthy display of list of commands. It actually displays everything; commands, aliases, functions etc,. To see only Functions, we combine Get-Command
and Where-Object
cmdlets.
Get-Command
results the data into CommandType, Name and ModuleName columns. These are the properties and we can pass these to our command. The command looks like below:
PS C:\> Get-Command | Where-Object CommandType -EQ Function CommandType Name ModuleName ----------- ---- ---------- Function A: Function B: Function C: Function cd.. Function cd\ Function Clear-Host Function Configuration PSDesiredStateConfiguration .................... .................... Function Z: PS C:\>
We passed the results of Get-Command
to Where-Object
and given the condition that, if the CommandType is a Function. Observe the results that, the above command displays only the functions which are available through PowerShell.
-EQ
is the equality operator we used to compare two values or operands. I will explain about these operators in a separate Article.
We discuss more as we go.
[..] David