Null coalescing in powershell

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Null coalescing in powershell

Message par ForumBot »

Null coalescing in powershell
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: Null coalescing in powershell

Message par ForumBot »

Powershell 7+

[Powershell 7 introduces](https://learn.microsoft.com/en-gb/powershell/scripting/whats-new/what-s-new-in-powershell-70?view=powershell-7#null-coalescing-assignment-and-conditional-operators) native null coalescing, null conditional assignment, and ternary operators in Powershell.

**Null Coalescing**

```
$null ?? 100 # Result is 100

"Evaluated" ?? (Expensive-Operation "Not Evaluated") # Right side here is not evaluated

```

**Null Conditional Assignment**

```
$x = $null
$x ??= 100 # $x is now 100
$x ??= 200 # $x remains 100

```

**Ternary Operator**

```
$true ? "this value returned" : "this expression not evaluated"
$false ? "this expression not evaluated" : "this value returned"

```

Previous Versions:

No need for the Powershell Community Extensions, you can use the standard Powershell if statements as an expression:

```
variable = if (condition) { expr1 } else { expr2 }

```

So to the replacements for your first C# expression of:

```
var s = myval ?? "new value";

```

becomes one of the following (depending on preference):

```
$s = if ($myval -eq $null) { "new value" } else { $myval }
$s = if ($myval -ne $null) { $myval } else { "new value" }

```

or depending on what $myval might contain you could use:

```
$s = if ($myval) { $myval } else { "new value" }

```

and the second C# expression maps in a similar way:

```
var x = myval == null ? "" : otherval;

```

becomes

```
$x = if ($myval -eq $null) { "" } else { $otherval }

```

Now to be fair, these aren't very snappy, and nowhere near as comfortable to use as the C# forms.

You might also consider wrapping it in a very simple function to make things more readable:

```
function Coalesce($a, $b) { if ($a -ne $null) { $a } else { $b } }

$s = Coalesce $myval "new value"

```

or possibly as, IfNull:

```
function IfNull($a, $b, $c) { if ($a -eq $null) { $b } else { $c } }

$s = IfNull $myval "new value" $myval
$x = IfNull $myval "" $otherval

```

As you can see a very

*(Réponse tronquée)*
Répondre

Revenir à « PowerShell »