Alternate value

Hello everybody,
In C#, exists a compact syntax to express a variable that can have a value or another, depending on the value of another variable.

For instance:
str = (a > b) ? "a greater than b" : "a not greater than b")

This will return in str either “a greater than b” or “a not greater than b”, depending on the comparison of a and b.

So, about this, I have two questions:

  • does this exist in PowerShell ?
  • how could I have asked this to Bing, without it answering me with an Excel function ?

Is this a code ?

Gloops,
Welcome to the forum. :wave:t3:

If I got you right you can use the ternary operator.

That’s a tricky question. But if Bing, Google or ChatGPT fail you can still ask humans … us … here … or StackOverflow or Reddit or … :wink: :man_shrugging:t3:

BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Oh well, before asking my question I spent half an hour trying this, and then I realised that perhaps this was not a PowerShell syntax.

PowerShell underlines the question mark in red, and tells me that it is an unexpected token.

I presume that this is a question of version of PowerShell ?
I was using Windows PowerShell, 5.1.22621.2428.

Well, before posting, I took the time to read a notice that explained I had to type two ticks before and two ticks after a short text, or three ticks alone on a line before, and the same after, when it is a code of several lines.

Did I use the bad ticks?
Or read the bad notice?

I just corrected my question to use the button.
It seems it requires some habit to see the difference.


Pardon me to insist, I used the Stylus extension to change the background of the code.
I see the same background for the text I formatted with the button, and for the one I framed with ticks (either one or two before and after).
It means that in the source of the page it is framed by < code> tags.

Oooops, sorry, I forgot to mention that. Yes, it is a feature we don’t have in Windows PowerShell (version 5.1). If you need it you have to use PowerShell core
(version 7.x.x). :man_shrugging:t3:

If you want to post a small piece of code inside plain text you quote it with a single backtick on each side. If you want to post a code block you place 3 consecutive backticks on an emptly line before and after the code block. Or you simply use the button of the post editor (</>). If you’ve selected a whole line of text/code it will create a code block. And if you just selected some characters on a line of text it will make the code inline.

Oh, you mean in PowerShell 5 I have got to write a function?

function iif
{
    param(
        $cond,
        [string] $str1,
        [string] $str2
    )
    if($cond -eq $true)
    {
        return $str1;
    }
    else
    {
        return $str2;
    }
}

iif($false, "yes", "no")

Oh, the function did not receive its string arguments, did I commit a mistake?
And if I try to declare [bool] as the type of the first parameter, I have syntax errors.
If I put “yes” or “no” in the code of the function without taking care of the arguments then I have the results, but it was not the aim.

Well, it seems the message was received the first time.

If you’re limitted to version 5.1 and since the ternary operator is actually just a shortcut for an if - else statement why not just use an if -else? :man_shrugging:t3:

And BTW:

It might be just for testing but it is best practice to name functions in PowerShell like cmdlets.

That’s not how we call functions in PowerShell. :wink:

If you want to use positional parameters you would call it like this:

iif $false "yes"  "no"

Oh, that was it?
Thenk you.