Skip to content

Instantly share code, notes, and snippets.

@elonmallin
Last active September 7, 2018 08:43
Show Gist options
  • Save elonmallin/2cc2ae1b0f59e23451e87a10f4316ecf to your computer and use it in GitHub Desktop.
Save elonmallin/2cc2ae1b0f59e23451e87a10f4316ecf to your computer and use it in GitHub Desktop.
Adds the null coalesc operator from C# to Powershell (As close as we get anyway)
<#
Adds the null coalescing operator.
Use e.g:
`$Color = $Settings.Color |?? "blue"`
#>
function NullCoalesc {
param (
[Parameter(ValueFromPipeline=$true)]$Value,
[Parameter(Position=0)]$Default
)
if ($Value) { $Value } else { $Default }
}
Set-Alias -Name "??" -Value NullCoalesc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment