I was also looking for a better answer, and although the solution as Edward is “normal”, I came up with a much more natural solution in this blog post
Short and sweet:
# --------------------------------------------------------------------------- # Name: Invoke-Assignment # Alias: = # Author: Garrett Serack (@FearTheCowboy) # Desc: Enables expressions like the C# operators: # Ternary: # <condition> ? <trueresult> : <falseresult> # eg # status = (age > 50) ? "old" : "young"; # Null-Coalescing # <value> ?? <value-if-value-is-null> # eg # name = GetName() ?? "No Name"; # # Ternary Usage: # $status == ($age > 50) ? "old" : "young" # # Null Coalescing Usage: # $name = (get-name) ? "No Name" # --------------------------------------------------------------------------- # returns the evaluated value of the parameter passed in, # executing it, if it is a scriptblock function eval($item) { if( $item -ne $null ) { if( $item -is "ScriptBlock" ) { return & $item } return $item } return $null } # an extended assignment function; implements logic for Ternarys and Null-Coalescing expressions function Invoke-Assignment { if( $args ) { # ternary if ($p = [array]::IndexOf($args,'?' )+1) { if (eval($args[0])) { return eval($args[$p]) } return eval($args[([array]::IndexOf($args,':',$p))+1]) } # null-coalescing if ($p = ([array]::IndexOf($args,'??',$p)+1)) { if ($result = eval($args[0])) { return $result } return eval($args[$p]) } # neither ternary or null-coalescing, just a value return eval($args[0]) } return $null } # alias the function to the equals sign (which doesn't impede the normal use of = ) set-alias = Invoke-Assignment -Option AllScope -Description "FearTheCowboy Invoke-Assignment."
Which makes it easy to do such things (more blog examples):
$message == ($age > 50) ? "Old Man" :"Young Dude"
Garrett Serack Dec 11 '15 at 19:21 2015-12-11 19:21
source share