title: How to use default value when given an empty string in powershell function?
body: <p>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 <code>$Var</code> is empty, and setting it to a default. Anyone know if this can be achieved? Also need to support powershell version 2.0.</p>
  
  <p>Below is a snippet of what I'm trying to accomplish. Given an empty string I would like it to print "Var: DEFAULT".</p>
  
  <pre><code>$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
  </code></pre>