Page tree

Welcome to FreeSoftwareServers Confluence Wiki

Skip to end of metadata
Go to start of metadata

https://ss64.com/ps/if.html

https://powershellexplained.com/2019-08-11-Powershell-if-then-else-equals-operator/

If ($fruit -eq "orange") {'We found an orange'}
ElseIf ($fruit -eq "apple") {'We found an apple'}
IF (!$DEFSERVER -OR $DUPSERVER) #IF NOT DEFINED OR DEFINED
IF ($DOUBLESIDED -eq "TRUE")
get-service | foreach-object {  
  if ($_.status -eq "stopped") { write-host -f red $_.name $_.status }
  else { write-host -f green $_.name $_.status }
}

Equals/Not Equals (Math or Text)

-eq case insensitive equality
-ieq case insensitive equality
-ceq case sensitive equality
-ne case insensitive not equal
-ine case insensitive not equal
-cne case sensitive not equal

Math Operators:

-gt greater than
-igt greater than, case insensitive
-cgt greater than, case sensitive
-ge greater than or equal
-ige greater than or equal, case insensitive
-cge greater than or equal, case sensitive
-lt less than
-ilt less than, case insensitive
-clt less than, case sensitive
-le less than or equal
-ile less than or equal, case insensitive
-cle less than or equal, case sensitive

Wildcard Search: (-like)

-like case insensitive wildcard
-ilike case insensitive wildcard
-clike case sensitive wildcard
-notlike case insensitive wildcard not matched
-inotlike case insensitive wildcard not matched
-cnotlike case sensitive wildcard not matched
? will match any single character
* will match any number of characters

Regex Search: (-match)

-match case insensitive regex
-imatch case insensitive regex
-cmatch case sensitive regex
-notmatch case insensitive regex not matched
-inotmatch case insensitive regex not matched
-cnotmatch case sensitive regex not matched

RegEx Pattern Building:

*    if ( $value -match 'S-\w\w\w-SQL\d\d')
\d digit [0-9]
\w alpha numeric [a-zA-Z0-9_]
\s whitespace character
.  any character except newline
() sub-expression
\  escape the next character

Type: (-is)

-is of type
-isnot not of type
if ( $value -is [string] )
  if ( $Service -isnot [System.ServiceProcess.ServiceController] )

Arrays:

    $array = 1..6
    if ( $array -gt 3 )
    $array = 1..6
    if ( $array -contains 3 )
-contains case insensitive match
-icontains case insensitive match
-ccontains case sensitive match
-notcontains case insensitive not matched
-inotcontains case insensitive not matched
-cnotcontains case sensitive not matched
    $array = 1..6
    if ( 3 -in $array )
-in case insensitive match
-iin case insensitive match
-cin case sensitive match
-notin case insensitive not matched
-inotin case insensitive not matched
-cnotin case sensitive not matched

Logical

-not
-and
-or
-xor # both items are $false or both items are $true, then the whole expression is $false

AND

    if ( ($age -gt 13) -and ($age -lt 55) )

EXTRA

if ( Get-Process Notepad* )
if ( Get-Process | Where Name -eq Notepad )
if ( (Get-Process) -and (Get-Service) )

NULL:

 if ( $null -eq $value )
  • No labels