Skip to content

Conditionals

Tags: powershell


Conditionals

TypeOperatorComparison test
Equalityeqequals
nenot equals
gtgreater than
gegreater than or equal
ltless than
leless than or equal
matchinglikestring matches wildcard pattern
notlikestring does not match wildcard pattern
matchstring matches regex pattern
notmatchstring does not match regex pattern
Replacementreplacereplaces strings matching a regex pattern
Containmentcontainscollection contains a value
notcontainscollection does not contain a value
invalue is in a collection
notinvalue is not in a collection
Typeisboth objects are the same type
isnotthe objects are not the same type

Where this or that

$pnpDevices | ? { $_.Status -Match "Error|Warning|Degraded|Unknown" } | Select Status,Name,InstanceID

If

If And

Terminal window
If (condition1 -And condition2) {Do stuff}
# An alternative explanation would be
If (test1 -And test2) {
execute block command when true
}

If not

Terminal window
If (-Not ($true)) {Do Stuff}
If (!($true)) {Do Stuff}