The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,859 other subscribers

Ternary operator in PowerShell – Stack Overflow

Posted by jpluimers on 2019/07/31

I like this built-in construct by fbehrens most:

$result = If ($condition) {"true"} Else {"false"}

Everything else is incidental complexity and thus to be avoided.

For use in or as an expression, not just an assignment, wrap it in $(), thus:

write-host $(If ($condition) {"true"} Else {"false"})

There are even more elegant constructs, but those require setting up an alias before using them.

Source: [WayBackTernary operator in PowerShell – Stack Overflow

–jeroen

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.