title: How to run a Powershell script from the command line and pass a directory as a parameter
body: <pre class="lang-powershell prettyprint-override"><code>PowerShell -Command .\Foo.ps1
  </code></pre>
  
  <ul>
  <li><p><code>Foo.ps1</code>:</p>
  
  <pre class="lang-powershell prettyprint-override"><code>Function Foo($directory)
  {
      echo $directory
  }
  
  if ($args.Length -eq 0)
  {
      echo "Usage: Foo &lt;directory&gt;"
  }
  else
  {
      Foo($args[0])
  }
  </code></pre>
  
  <p>Despite <code>Foo.ps1</code> being in the directory from where I am calling Powershell, this results in:</p>
  
  <pre class="lang-none prettyprint-override"><code>The term '.\Foo.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. 
  Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
  </code></pre>
  
  <ul>
  <li><em>EDIT: Wasn't working because PowerShell was changing directory due to <code>profile.ps1</code> containing <code>cd C:\</code></em></li>
  </ul></li>
  </ul>
  
  <p><br> I then tried to call it specifying the full path to the script file, but no matter what I try, I can't get it to work. <em>I believe I have to quote the path because it contains whitespaces, as does the file name I need to pass in an argument to the script.</em></p>
  
  <ul>
  <li><p>Best guess so far:</p>
  
  <pre class="lang-powershell prettyprint-override"><code>PowerShell -Command "'C:\Dummy Directory 1\Foo.ps1' 'C:\Dummy Directory 2\File.txt'"
  </code></pre>
  
  <p>Outputs error: </p>
  
  <pre class="lang-none prettyprint-override"><code>Unexpected token 'C:\Dummy Directory 2\File.txt' in expression or statement. 
  At line:1 char:136.
  </code></pre></li>
  </ul>