3

I would like to use the default value when given an empty string. I am hoping for something more elegant then having an if statement to check if $Var is empty, and setting it to a default. Anyone know if this can be achieved? Also need to support powershell version 2.0.

Below is a snippet of what I'm trying to accomplish. Given an empty string I would like it to print "Var: DEFAULT".

$ErrorActionPreference = "Stop"

function Test-Function(
  [parameter( Mandatory = $true )][string] $Var = "DEFAULT"
) {
  # If Mandatory is set to $true an error is thrown
  # "Cannot bind argument to parameter 'password' because it is an empty string."
  # When Mandatory is set to $false, $Var is an empty string rather than "DEFAULT"

  Write-Host "Var: $Var"
}

$EmptyString = ""
Test-Function -Var $EmptyString

3 Answers 3

4

Var is an empty string because you are explicitly passing an empty string. Empty strings are still objects, not null. Your call to Test-Function -Var $EmptyString fails to give you the output you are looking for as you are equating an empty string and null which is false in .Net. Your statement that "When Mandatory is set to $false, $Var is an empty string rather than 'DEFAULT'" is correct as you did pass something, an empty string so the assignment of the value "DEFAULT" was never called.

You could remove the Mandatory=$true in which case your "Default" value is displayed when the parameter is not passed.

function Test-Function(
  [parameter( Mandatory = $false )]
  [string] $Var = "DEFAULT"
  ){
  Write-Host "Var: $Var"
 }
Test-Function

This generates Var: DEFAULT as expected.

Regardless of whether the parameter is mandatory or not, if you pass an empty string the assignment to $Var = "Default" is never reached as $Var has a value of '' which while empty is actually a string.

Test-Function ''

This generates Var: Which may look like wrong but it output the empty string you told it to.

If you want to allow a default value to be assigned when the parameter is not passed use the Mandatory=$false and assign a default value as I did above. If you want to test the value that was passed and assign a default value if it was an empty string you should do that in the begin block of your function.

function Test-Function(
  [parameter( Mandatory = $false )]
  [string] $Var = "DEFAULT"
  ){
  begin{if([String]::IsNullOrWhiteSpace($Var)){$Var="DEFAULT"}}
  process{Write-Host "Var: $Var"}
  end{}
 }
Test-Function
Test-Function ''
Test-Function 'SomeValue'

This generates the following which I believe to be what you expected:

Var: DEFAULT
Var: DEFAULT
Var: SomeValue
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your response. So I guess what I am looking to do is not possible without using an if statement if I always want to try and pass in a $Var.
2

There is no elegant way to do that. At least there is not a built-in way to do so. You can just validate that the string is not null or empty:

function Test {
    Param (
        [ValidateNotNullOrEmpty()]
        [String]
        $String = 'DEFAULT'
    )

    Write-Host -Object "String: $String"
}

2 Comments

This doesn't use the default value. Throws an exception indicating the parameter is either null or empty. Instead of throwing this exception I'd like the default value to be used.
"There is no elegant way to do that. At least there is not a built-in way to do so. You can just validate that the string is not null or empty"... with '[ValidateNotNullOrEmpty()]'
1

Use this inside your function:

If (!($var)) {$var = "DEFAULT"} #basically, if $var doesn't exist, create it

ps. I've just tested it with Mandatory = $false and it works as expected, no idea why it doesn't work for you, what does it do when mandatory = $false?

2 Comments

When Mandatory = $false the output is just Var: when I expect Var: DEFAULT
for me it works perfectly, just like i said. but anyway, my answer fits your purpose, try it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.