pipes

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 15 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
rscarlson
Posts: 3
Last visit: Wed Sep 03, 2008 2:05 am

pipes

Post by rscarlson »


I seem to be able to pipe conditional statements like:
gwmi -class "win32_service" -namespace "rootCIMV2" | Where-Object {$_.StartMode -eq "Auto" -and $_.Status -eq "OK"}
or:
gwmi -class "win32_service" -namespace "rootCIMV2" | Where-Object {$_.StartMode -eq "Auto" -or $_.Status -eq "OK"}
But how do I use the "-not" condition?
I can use something like:
$colitems = gwmi -class "win32_service" -namespace "rootCIMV2" `
foreach ($objItem in $colItems) {
$a = $objItem.Name
# write-host "Name: " $a
$b = $objItem.StartMode
# write-host "StartMode: " $b
$c = $objItem.Status
# write-host "State: " $c
$d = $objItem.State
# write-host "Status " $d
If ($b -ne "Manual")
{ If ($d -ne "Running")
{
Write-Host $a "`t" $b "`t" $c "`t" $d
}
}
}
Is there a better way?
Roy C
User avatar
rscarlson
Posts: 3
Last visit: Wed Sep 03, 2008 2:05 am

pipes

Post by rscarlson »


I seem to be able to pipe conditional statements like:
gwmi -class "win32_service" -namespace "rootCIMV2" | Where-Object {$_.StartMode -eq "Auto" -and $_.Status -eq "OK"}
or:
gwmi -class "win32_service" -namespace "rootCIMV2" | Where-Object {$_.StartMode -eq "Auto" -or $_.Status -eq "OK"}
But how do I use the "-not" condition?
I can use something like:
$colitems = gwmi -class "win32_service" -namespace "rootCIMV2" `
foreach ($objItem in $colItems) {
$a = $objItem.Name
# write-host "Name: " $a
$b = $objItem.StartMode
# write-host "StartMode: " $b
$c = $objItem.Status
# write-host "State: " $c
$d = $objItem.State
# write-host "Status " $d
If ($b -ne "Manual")
{ If ($d -ne "Running")
{
Write-Host $a "`t" $b "`t" $c "`t" $d
}
}
}
Is there a better way?
Roy C
User avatar
rscarlson
Posts: 3
Last visit: Wed Sep 03, 2008 2:05 am

pipes

Post by rscarlson »

Thanks for the reply, I agree with using the query.
Still curious about using the Where-Object (and other piped) with the "-nor" conditional for applications where a query does not apply.

Roy
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

pipes

Post by jhicks »

Let me give you some examples to try. These aren't necessarily perfect production expressions but should serve to demonstrate how to use the operators.

get-process | where {$_.company -match "microsoft" -AND $_.name -ne "svchost"}

get-process | where {$_.company -notmatch "microsoft" -or $_.company -match "google"} | Sort Company | Select name,Company

get-service | where {$_.status -eq "Running" -AND -not $_.CanStop}

I also encourage you to look at help about_logical_operator and help about_comparison_operators
This topic is 15 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked