Skip to content

Instantly share code, notes, and snippets.

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