0

In Bash, I can do something like:

${VAR1:-default}

...which means if VAR1 doesn't exist, return "default".

There are lots of these types of operators:Bash parameter expansion examples

There seem to be numerous examples using Get-Variable but nothing particularly succinct. Is there an equivalent in Powershell that I'm missing?

DaveStephens
  • 1,126
  • 12
  • 16
  • AFAIK PowerShell doesn't support this kind of parameter expansion. What are you trying to achieve? Do you need this for function parameters or for variables in general? – Ansgar Wiechers Jun 28 '17 at 09:31
  • FWIW If that's inside a string you use as a template, you can make your own template processing function that behaves just like Bash. – wOxxOm Jun 28 '17 at 09:32

1 Answers1

1

If you need defaults when processing the arguments to your script or function, then yes you can supply them:

param (
    [string]$price = 100, 
    [string]$ComputerName = $env:computername,    
    [string]$username = $(throw "-username is required."),
    [string]$password = $( Read-Host -asSecureString "Input password" )
    [switch]$SaveData = $false
)

but if you mean defaults when substituting an existing variable into an expression then no, you have to write an expression that gives you the desired value.

Duncan
  • 92,073
  • 11
  • 122
  • 156