top of page

PowerShell If & Else

 # A Basic If and Else Conditional Statement        $MyNumber="299"
                
        If ($MyNumber -gt 200){
        
            Write-host "The Value is Above 200"            
        
        }Else{
        
            Write-Host "The Value is Not Above 200"

        }         

ifelse2.PNG

# Just a bit more complex with a If and a Elseif for doing 2 conditionals. 

     
        $Myteststring = "ScottHead"
        
        If ($Myteststring.StartsWith("M")){
        
            Write-host "It starts with an M"            
        
        }Elseif($Myteststring.StartsWith("S")){
        
            Write-Host "It starts with an S"

        }         

Elseif.PNG

# This is called a Nested If Statement       
        $MyNumber="299"
                      
        If ($MyNumber -gt 200){
        
           If($MyNumber -lt 300){

            Write-Host "The number is above 200 and less than 300"

           }                     
        
        }     

IfNested.PNG
bottom of page