Assignment within if statements

An if statement can include an assignment step, as follows:

if ($i = 1) { 
    Write-Host "Implicit true. The variable i is $i" 
} 

This is most commonly used when testing for the existence of a value in a variable, for example:

if ($interface = Get-NetAdapter | Where-Object Status -eq 'Up') { 
    Write-Host "$($interface.Name) is up"
}

In the previous example, the statement to the right of the assignment operator (=) is executed, assigned to the $interface variable, and then the value in the variable is treated as an implicit Boolean.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.104.67