Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Sign InSign Up

Softans

Softans Logo Softans Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
Home/ Questions/Q 1286
In Process
Anonymous
Anonymous
Asked: January 27, 20222022-01-27T09:32:34+00:00 2022-01-27T09:32:34+00:00

PowerShell inline If (IIf)

How do I create an a statement with an inline If (IIf, see also: Immediate if or ternary If) in PowerShell?

The Microsoft Connect is retired, but the good news is that support for a ternary operator in PowerShell (Core) language appears to be on its way

inline if (iif)powershell
  • 0
  • 1 1 Answer
  • 22 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  1. Ghulam Nabi
    2022-01-27T09:32:49+00:00Added an answer on January 27, 2022 at 9:32 am

    You can use the PowerShell’s native way:

    "The condition is " + (&{If($Condition) {"True"} Else {"False"}}) + "."
    

    But as this adds a lot of parenthesis and brackets to your syntax, you might consider the following (probably one of the smallest existing) CmdLet:

    Function IIf($If, $Right, $Wrong) {If ($If) {$Right} Else {$Wrong}}
    

    Which will simplify your command to:

    "The condition is " + (IIf $Condition "True" "False") + "."
    

    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:

    Function IIf($If, $IfTrue, $IfFalse) {
        If ($If) {If ($IfTrue -is "ScriptBlock") {&$IfTrue} Else {$IfTrue}}
        Else {If ($IfFalse -is "ScriptBlock") {&$IfFalse} Else {$IfFalse}}
    }
    

    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:

    IIf $a {1/$a} NaN
    

    Or placed inline:

    "The multiplicative inverse of $a is $(IIf $a {1/$a} NaN)."
    

    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:

    If ($Object) {$a = $Object.Method()} Else {$a = $null}
    

    (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:

    $a = IIf $Object {$Object.Method()}
    

    (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):

    Function IIf($If, $Then, $Else) {
        If ($If -IsNot "Boolean") {$_ = $If}
        If ($If) {If ($Then -is "ScriptBlock") {&$Then} Else {$Then}}
        Else {If ($Else -is "ScriptBlock") {&$Else} Else {$Else}}
    }
    

    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:

    $VolatileEnvironment = Get-Item -ErrorAction SilentlyContinue "HKCU:\Volatile Environment"
    $UserName = IIf $VolatileEnvironment {$_.GetValue("UserName")}
    

    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.:

    $RegistryKeys | ForEach {
        $UserName = IIf ($Number -lt 5) {$_.GetValue("UserName")}
    }
    

    Or:

    $RegistryKeys | ForEach {
        $UserName = IIf [Bool]$VolatileEnvironment {$_.OtherMethod()}
    }
    

    Added 2020-03-20:

    Using the ternary operator syntax

    PowerShell 7.0 introduced a new syntax using the ternary operator. It follows the C# ternary operator syntax:

    The ternary operator behaves like the simplified if-else statement. The <condition> expression is evaluated and the result is converted to a boolean to determine which branch should be evaluated next:

    The <if-true> expression is executed if the <condition> expression is true The <if-false> expression is executed if the <condition> expression is false

    Example:

    "The multiplicative inverse of $a is $($a ? (& {1/$a}) : 'NaN')."
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • Popular
  • Answers
  • Ghulam Nabi

    Why are the British confused about us calling bread rolls ...

    • 5 Answers
  • Alex

    application has failed to start because no appropriate graphics hardware ...

    • 4 Answers
  • Jerry

    Add file to native target programmatically via tuist/XcodeProj

    • 4 Answers
  • Ghulam Nabi
    Ghulam Nabi added an answer To resolve the NullPointerException, you need to identify the variable… March 15, 2023 at 8:25 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can replace the PnP code in your Azure Function… February 13, 2023 at 7:11 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can use the $match stage in the aggregate pipeline… February 10, 2023 at 6:20 am

Trending Tags

android c++ cypress flutter java javascript python selenium testng webdriver

Top Members

Robert

Robert

  • 3 Questions
  • 1k Points
Luci

Luci

  • 5 Questions
  • 1k Points
Kevin O Brien

Kevin O Brien

  • 2 Questions
  • 1k Points

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Softans

Softans is a social questions & Answers Engine which will help you establish your community and connect with other people.

About Us

  • Blog
  • Jobs
  • About Us
  • Meet The Team
  • Contact Us

Legal Stuff

Help

Follow

© 2021 Softans. All Rights Reserved
With Love by Softans.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.