February 2019
Intermediate to advanced
626 pages
15h 51m
English
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.
Read now
Unlock full access