Filtering output using "Where-Object" in Powershell

I am trying to get into PowerShell and ran into my first hurdle.

when i run

Get-Command | Where-Object CommandType -contains Cmdlet 

My output is filtered so that only commands with the value of the CommandType property containing "Cmdlet" are displayed, for example:

enter image description here

You can do the same with the Source object:

 Get-Command | Where-Object Source -contains appx 

Which gets me:

enter image description here

But when I try to run:

 Get-Command | Where-Object Name -contains Add 

I get nothing. Why can I filter the output using the "CommandType" and "Source, but not the" Name "objects? Of course, I missed something here ...

Edit: I know I can run:

 Get-Command -verb "get" 

And get the desired result. But I'm trying to understand why my where-object statement does not work.

Edit 2:

Obviously, if I use the comparison operator "-match", it works ...

 get-command | where-object Name -match "add" 

But aren't the "name" properties just strings? -match should be used to compare afaik regular expressions? I'm so confused now ...

+6
source share
1 answer

use either the like operator or match :

 Get-Command | Where-Object Name -like Add* 

this will match the addition of the word anywhere

 Get-Command | Where-Object Name -match Add 

but the best way to do this is:

 Get-Command -verb Add 

More information about the operator can be found here.

-contains Description: Containment operator. Reports whether the reference collection values ​​include one test value. Always returns a boolean value. It returns TRUE only when the test value exactly matches at least one of the reference values.

  PS C:\> "abc", "def" -Contains "def" True 
+8
source

All Articles