title: PowerShell & MSDeploy - Arguments with Spaces
body: <p>I cannot work out how to pass arguments that contain folders with spaces using msdeploy.exe and PowerShell v4.</p>
  
  <p>Sample Powershell Script</p>
  
  <pre class="lang-ps prettyprint-override"><code>write-warning "WITHOUT SPACE"
  $fl1 = "d:\nospace\a.txt"
  $fl2 = "d:\nospace\b.txt"
  
  $arg1 = "-source:filePath=`"$fl1`""
  $arg2 = "-dest:filePath=`"$fl2`""
  
  msdeploy.exe "-verb:sync",$arg1,$arg2
  
  write-warning "WITH SPACE"
  $fl1 = "d:\space space\a.txt"
  $fl2 = "d:\space space\b.txt"
  
  $arg1 = "-source:filePath=`"$fl1`""
  $arg2 = "-dest:filePath=`"$fl2`""
  
  msdeploy.exe "-verb:sync",$arg1,$arg2
  </code></pre>
  
  <p>When the folder name has no spaces, it works fine, however when it has a space it fails:</p>
  
  <pre class="lang-none prettyprint-override"><code>msdeploy.exe : Error: Unrecognized argument '"-source:filePath="d:\space'. All arguments must begin with "-".
  At E:\PAWS\Payroll System\PES-Branch-FW\Publish\DeployPackage.ps1:253 char:9
  +         msdeploy.exe "-verb:sync",$arg1,$arg2
  +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  + CategoryInfo          : NotSpecified: (Error: Unrecogn...begin with "-".:String) [], RemoteException
  + FullyQualifiedErrorId : NativeCommandError
  
  Error count: 1.
  </code></pre>
  
  <p>Manually calling msdeploy.exe using the following command:</p>
  
  <p><code>msdeploy -verb:sync -source:filePath="d:\space space\a.txt" -dest:filePath="d:\space space\b.txt"</code></p>
  
  <p>This works fine from Command Prompt but does not work from PowerShell.</p>
  
  <p>I have used this blog as an aid but without any luck: <a href="http://trycatchfail.com/blog/post/The-trials-and-tribulations-of-using-MSDeploy-with-PowerShell.aspx" rel="nofollow noreferrer">http://trycatchfail.com/blog/post/The-trials-and-tribulations-of-using-MSDeploy-with-PowerShell.aspx</a></p>
  
  <h2>Update</h2>
  
  <p>I have looked into some more examples. If you perform a standard copy operation powershell is able to pass the path to cmd.exe (copy).</p>
  
  <pre><code>write-warning "WITHOUT SPACE"
  $fl1 = "d:\nospace\a.txt"
  $fl2 = "d:\nospace\b.txt"
  
  $args = ('"{0}" "{1}"' -f $fl1, $fl2)
  write-host $args
  cmd /c copy $args
  
  write-warning "WITH SPACE"
  $fl1 = "d:\space space\a.txt"
  $fl2 = "d:\space space\b.txt"
  
  $args = ('"{0}" "{1}"' -f $fl1, $fl2)
  write-host $args
  cmd /c copy $args
  </code></pre>
  
  <p>Using the same approach to update the msdeploy snippet still fails because of the space.</p>
  
  <pre><code>write-warning "WITHOUT SPACE"
  $fl1 = "d:\nospace\a.txt"
  $fl2 = "d:\nospace\b.txt"
  
  $arg1 = '-source:filePath="{0}"' -f $fl1
  $arg2 = '-dest:filePath="{0}"' -f $fl2
  
  $args = '-verb:sync',$arg1, $arg2
  msdeploy.exe $args
  
  write-warning "WITH SPACE"
  $fl1 = "d:\space space\a.txt"
  $fl2 = "d:\space space\b.txt"
  
  $arg1 = '-source:filePath="{0}"' -f $fl1
  $arg2 = '-dest:filePath="{0}"' -f $fl2
  
  $args = '-verb:sync',$arg1, $arg2
  msdeploy.exe $args
  </code></pre>
  
  <h2>One Solution</h2>
  
  <p><a href="https://stackoverflow.com/a/12813048/1497635">https://stackoverflow.com/a/12813048/1497635</a></p>
  
  <p>I would like to add that three escape characters is absolutely crazy. There must be a neater solution to the problem.</p>