Jun 30 2018 2:05PM GMT
Tags:
In this video, we do an introduction to using the PowerShell If statement and all of the nooks and crannies that go along with it. We look at If, ElseIf, and If and how you can get the most out of.
Tags:
Thanks! We'll email you
when relevant content is
added and updated.
Powershellwhen relevant content is
added and updated.
When you’re using an if statement you’re usually testing for a positive so how do you do a PowerShell if not
There a few scenarios to cover. The simplest is if you’re testing a boolean:
PS> $x = $true
if ($x) {‘Yes’} else {‘No’}
Yes
Yes
In an if statement the test ($x) is evaluated and must give a true or false answer. If true then Yes else No
Let’s make turn the test into a not test. You can use ! or –not as you prefer
PS> $x = $true
if (!$x) {‘Yes’} else {‘No’}
if (-not $x) {‘Yes’} else {‘No’}
No
No
if (-not $x) {‘Yes’} else {‘No’}
No
No
If the value is already false
PS> $x = $false
if (!$x) {‘Yes’} else {‘No’}
if (-not $x) {‘Yes’} else {‘No’}
Yes
Yes
if (-not $x) {‘Yes’} else {‘No’}
Yes
Yes
Be careful as you’re getting into double negative territory which is always a headache when you come to review the code at some time in the future.
If you’re dealing with numeric values
PS> $x = 5
if ($x -ne 5) {‘Yes’} else {‘No’}
if ($x -lt 5) {‘Yes’} else {‘No’}
if ($x -gt 5) {‘Yes’} else {‘No’}
No
No
No
if ($x -lt 5) {‘Yes’} else {‘No’}
if ($x -gt 5) {‘Yes’} else {‘No’}
No
No
No
Be careful with the first one as you’ll only get Yes if $x –ne 5. Back to double negative thinking.
Notice you can’t do this
if ($x -not -lt 5) {‘Yes’} else {‘No’}
Double operators don’t work. You get an error about
At line:1 char:8
+ if ($x -not -lt 5) {‘Yes’} else {‘No’}
+ ~~~~
Unexpected token ‘-not’ in expression or statement.
At line:1 char:8
+ if ($x -not -lt 5) {‘Yes’} else {‘No’}
+ ~~~~
Unexpected token ‘-not’ in expression or statement.
At line:1 char:8
among other things.
Nulls can be tricky
PS> $x = $null
if ($x) {‘Yes’} else {‘No’}
if (!$x) {‘Yes’} else {‘No’}
if (!$x) {‘Yes’} else {‘No’}
No
Yes
Yes
A null value will evaluate as false unless you –not it – another double negative situation.
Using if not is relatively straight forward. Just make sure you have the logic thought through to deal with double negatives correctly.
How do I create an a statement with an Inline If (IIf, see also: http://en.wikipedia.org/wiki/IIf or ternary If) in PowerShell?
If you also think that this should be a native PowerShell function, please vote this up: https://connect.microsoft.com/PowerShell/feedback/details/1497806/iif-statement-if-shorthand
iRon
iRoniRon4,84022 gold badges2424 silver badges3838 bronze badges
7 Answers
You can use the PowerShell’s native way:
But as this adds a lot of parenthesis and brackets to your syntax, you might consider the follow (probably one of the smallest existing) CmdLet:
Which will simplify your command to:
Added 2014-09-19:
I have been using the
IIf
cmdlet now for a while and I still think it will make syntaxes more readable in a lot of cases but as I agree with Jason’s note about the unwanted side effect that both possible values will be evaluated even obviously only one value is used, I have changed the IIf
cmdlet a bit:Now you might add a ScriptBlock (surrounded by
{}
's) instead of an object which will not be evaluated if it is not required as shown in this example:Or placed inline:
In case
$a
has a value other than zero, the multiplicative inverse is returned; otherwise, it will return NaN
(where the {1/$a}
is not evaluated).Another nice example where it will make a quiet ambiguous syntax a lot simpler (especially in case you want to place it inline) is where you want to run a method on an object which could potentially be
$Null
.The native ‘If
’ way to do this, would be something like this:(Note that the
Else
part is often required in e.g. loops where you will need to reset $a
.)With the
IIf
cmdlet it will look like this:(Note that if the
$Object
is $Null
, $a
will automatically be set to $Null
if no $IfFalse
value is supplied.)Added 2014-09-19:
Minor change to the
IIf
cmdlet which now sets the current object ($_
or $PSItem
):This means you can simplify a statement (the PowerShell way) with a method on an object that could potentially be
$Null
.The general syntax for this will now be
$a = IIf $Object {$_.Method()}
. A more common example will look something like:Note that the command
$VolatileEnvironment.GetValue('UserName')
will normally result in an 'You cannot call a method on a null-valued expression.' error if the concerned registry (HKCU:Volatile Environment
) doesn’t exist; where the command IIf $VolatileEnvironment {$_.GetValue('UserName')}
will just return $Null
.If the
$If
parameter is a condition (something like $Number -lt 5
) or forced to a condition (with the [Bool]
type), the IIf
cmdlet won't overrule the current object, e.g.:Or:
iRoniRon4,84022 gold badges2424 silver badges3838 bronze badges
mjolinormjolinor51.6k33 gold badges7373 silver badges103103 bronze badges
Actually Powershell gives back values that haven't been assigned
Example:
Let's try it out:
Ivan AkcheurovIvan Akcheurov
ojkojk
HeyNowHeyNow
Frome here: https://blogs.msdn.microsoft.com/powershell/2006/12/29/diy-ternary-operator/
and then you can use that like this:
This is the most close variant I have seen so far.
Michael HafnerMichael Hafner
PowerShell doesn't have support for inline ifs. You'll have to create your own function (as another answer suggests), or combine if/else statements on a single line (as another answer also suggests).
Aaron JensenAaron Jensen15.1k99 gold badges6262 silver badges7575 bronze badges