Home > Software design >  PowerShell filtering twice on the same property
PowerShell filtering twice on the same property

Time:02-16

I would like to filter the content of a variable by two criteria referring to the same property. I ran into some struggle finding the correct syntax.

It looks like this:

$g = $allsites | Where-Object {($_.Name -like "[g]*") -and ($_.Name -notlike "[GRP-]*)"}

What I'm trying to achieve: Create the variable $g with the filtered content of the variable $allsites where the Site Name starts with with the letter "G" no matter the following letters. This result has to be filtered again. The content of the variable $g should contain site which are not named starting the expression "GRP-" only.

CodePudding user response:

The construct [GRP-] describes a character set, so you're instructing PowerShell to test if the name doesn't start with either G, R, P or -. Since you've already ensured that all names start with g already, this won't match any of them.

Change the pattern to just GRP-*:

$g = $allsites | Where-Object {$_.Name -like "G*" -and $_.Name -notlike "GRP-*"}
  • Related