title: How to run PowerShell Invoke-Command from Command Prompt when the script has a switch?
body: <p>I have the need to run PowerShell scripts using the command prompt (calling <code>powershell.exe</code> with the <code>-c</code> parameter).  I have run these for years but have never tried it with <code>Invoke-Command</code> in order to do remoting and when the script I want to execute has a switch I have to pass.</p>
  
  <p>This piece of code works great from PowerShell, a simple script that has the LogRun switch:</p>
  
  <pre><code>icm -cn MYCOMPUTER02 {C:\Temp\Write-to-File.ps1 -LogRun $Using:LogRun}
  </code></pre>
  
  <p>However, when I run it via the Command Prompt with <code>powershell.exe</code> fails:</p>
  
  <pre><code>powershell -c "icm -cn MYCOMPUTER02 {C:\Temp\Write-to-File.ps1 -LogRun $Using:LogRun}"
  </code></pre>
  
  <p>I have tried many variations of this and nothing works, I am sure I missing some syntax detail or some quotes somewhere.</p>
  
  <p>Please let me know if you can help me figure this out.</p>
  
  <p>This is the error I get:</p>
  
  <pre>
  icm : The value of the using variable '$using:LogRun' cannot be retrieved
  because it has not been set in the local session.
  At line:1 char:1
  + icm -cn MYCOMPUTER02{D:\PatV2DU\PatV2DUService20180411120032\Sc ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [Invoke-Command], RuntimeException
      + FullyQualifiedErrorId : UsingVariableIsUndefined,Microsoft.PowerShell.Commands.InvokeCommandCommand
  </pre>