24

I'm trying to get into PowerShell and have encountered my first hurdle.

when I run

Get-Command | Where-Object CommandType -contains Cmdlet

My output gets filtered so that only commands with "CommandType" property value containing "Cmdlet" gets shown, like so:

enter image description here

Same thing can be done with the object "Source":

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 by the objects "CommandType", and "Source but not "Name"? I'm surely missing something here...

Edit: i know i can run:

Get-Command -verb "get"

And get the desired output. But i'm trying to figure out why my "where-object" statement did not work.

Edit 2:

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

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

But isn't "name" properties just strings? -match should be used for Regular expression comparison afaik? I'm so confused right now...

1 Answer 1

33

use either the like or the match operator:

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

this will match add anywhere in the word

Get-Command | Where-Object Name -match Add

but a better way to do this would be:

Get-Command -verb Add

read more about the contains operator here

-Contains Description: Containment operator. Tells whether a collection of reference values includes a single test value. Always returns a Boolean value. Returns TRUE only when the test value exactly matches at least one of the reference values.

      PS C:\> "abc", "def" -Contains "def"
      True
2
  • 2
    Oh, I see now... -contains only returns true if the exact value is contained in a group of objects. I thought each individual object was parsed for an inline match. Thanks. Commented Feb 11, 2016 at 8:46
  • right...name property returns a single string which is passed to where and must be exactly equal to the string being compared to Commented Feb 11, 2016 at 8:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.