title: Powershell Invoke-Command not working in script
body: <p>I'm a beginner with Powershell and while working on a script I ran into a very weird issue.
  I have the following code which works as expected:</p>
  
  <pre><code>$deploymentMachine = Read-Host "Please enter the name of the machine on which you want to deploy: "
  $deploymentMachineUsername = Read-Host "Please enter the username: "
  $deploymentMachinePassword = Read-Host "Please enter your password: " -AsSecureString
  $credentials = New-Object System.Management.Automation.PSCredential ($deploymentMachineUsername, $deploymentMachinePassword)
  $remoteSession = New-PSSession -ComputerName $deploymentMachine -Credential $credentials -ErrorAction Stop
  </code></pre>
  
  <p>and then I'm trying to check for the existence of a folder on the remote machine using:</p>
  
  <pre><code>if(Invoke-Command -Session $remoteSession { Test-Path "C:\Test" })
  </code></pre>
  
  <p>which throws the following error:</p>
  
  <blockquote>
    <p>Cannot bind argument to parameter 'Path' because it is null.</p>
  </blockquote>
  
  <p>If I run the same code line by line in the Powershell everything works as expected and get true or false depending on what folder I test.</p>
  
  <p>Can somebody tell me why the code fails in the script, but works in the command prompt? What am I missing?</p>
  
  <p>Thank you!</p>