Null coalescing in powershell

后端 未结 10 1133
遇见更好的自我
遇见更好的自我 2020-11-29 21:57

Is there a null coalescing operator in powershell?

I\'d like to be able to do these c# commands in powershell:

var s = myval ?? \"new value\";
var x         


        
10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 22:21

    If you install the Powershell Community Extensions Module then you can use:

    ?? is the alias for Invoke-NullCoalescing.

    $s = ?? {$myval}  {"New Value"}
    

    ?: is the alias for Invoke-Ternary.

    $x = ?: {$myval -eq $null} {""} {$otherval}
    

提交回复
热议问题