title: Declaring variable in IF statement
body: <p>Why won't this work?</p>
  
  <pre><code>if (([datetime] $a = Date),$a.DayOfWeek -ne 'Wednesday'){
      Write-Host 1
      Exit
  }ElseIf ($a.hour -ne 9){
      Write-Host 2
      Exit
  }
  
  Do stuff...
  </code></pre>
  
  <p>but this will</p>
  
  <pre><code>if (($connected = Test-Connection 1.1.1.1 -Quiet), $connected -eq $false){
      Start-Process msg -ArgumentList "$env:USERNAME Not Connected..."
      Exit
  }
  
  Do stuff..
  </code></pre>
  
  <p>In the first example my script always exits unless I move <code>[datetime] $a = date</code> outside if statement. However, the second example works without issue.</p>
  
  <p>Also, if insert <code>$a</code> in the first if statement it returns the correct <code>datetime</code>.</p>