Example
PS C:\> get-command -verb format
CommandType Name Version Source
----------- ---- ------- ------
Function Format-Hex 3.1.0.0 Microsoft.PowerShell.Utility
Function Format-Volume 2.0.0.0 Storage
Cmdlet Format-Custom 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Format-List 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Format-SecureBootUEFI 2.0.0.0 SecureBoot
Cmdlet Format-Table 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Format-Wide 3.1.0.0 Microsoft.PowerShell.Utility
However, I also get the commands whose commandType is Function which I don't want to see. I can exclude them by
pipelining them into the
where-object
:
PS C:\> get-command -verb format | where-object commandType -eq cmdLet
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Format-Custom 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Format-List 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Format-SecureBootUEFI 2.0.0.0 SecureBoot
Cmdlet Format-Table 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Format-Wide 3.1.0.0 Microsoft.PowerShell.Utility
I am much happier now with the result. But still, I want to get rid of the format-secureBootUEFI
cmdLet because it does not fit in the list. So I need to add a second criteria.
It seems that
PowerShell is not capable of specifying multiple criterias without
script block. So, I use a script block for the second criteria.
PS C:\> get-command -verb format | where-object { $_.commandType -eq 'cmdLet' -and $_.source -match 'PowerShell' }
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Format-Custom 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Format-List 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Format-Table 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Format-Wide 3.1.0.0 Microsoft.PowerShell.Utility
Note: when using script blocks, the object on which the criterias are performed needs to be referenced by the
automatic variable $_
.
Removing elements from an array
The following example removes elements from an array that contains numbers. Elements that contain numbers are matched by the
regular expression \d
whose meaning is reversed with the
-notMatch
operator.
'foo','bar', 'hello world', 'the number is 42.', 'baz' | where-object {$_ -notMatch '\d' }