Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PowerShell filtering twice on the same property

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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-*"}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading