Nico1300
Nico1300

Reputation: 115

Powershell matching by two values

I try to get all Processes with starting letter w or s

Something like this:

Get-Process | Where-Object ProcessName -Match "^w.*" -or ProcessName -Match "^s.*" | Select-Object name, ID

but this doesnt work.

Upvotes: 0

Views: 700

Answers (3)

boxdog
boxdog

Reputation: 8442

Get-Process supports wildcards in the ProcessName parameter, so in this case you don't need Where-Object:

Get-Process -ProcessName '[ws]*' | Select-Object Name, ID

Upvotes: 1

zett42
zett42

Reputation: 27796

For complex expressions that use the same operator more than once or require logical operators, you need to pass a script block to Where-Object:

Get-Process | Where-Object { $_.ProcessName -Match "^w.*" -or $_.ProcessName -Match "^s.*" } | Select-Object name, ID

Though in this case, you don't really need a complex expression, as Mathias R. Jessen has pointed out.

Upvotes: 2

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174990

Since it's either one or the other, in the same position, you can use character class group ([characters]):

Get-Process | Where-Object ProcessName -match '^[ws]' | ...

The .* at the end of either pattern is moot.

Upvotes: 1

Related Questions