title: Function parameter validation in Powershell
body: <p>Why does a [string] casted parameter with the 'value' $null in this example never throw an error (empty or $null), but a string with the value '$null' always throws? I would expect if passing a mandatory parameter, it is checked for $null/emptyness and thus an error is always thrown in these cases:</p>
  <pre><code>Function test_M_NoE ( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string] $x ) {}
  
  # test cases. Uncomment one:
  [string]$x = [string]$null 
  # $x = [string]$null
  # [string]$x = $null
  # $x = $null 
  
  &quot;1:&quot;; test_M_NoE [string]$x # never error
  &quot;2:&quot;; test_M_NoE $x         # always error
  </code></pre>