title: How to run Start-Process in Powershell using user credentials?
body: <p>I've got a Windows service (Jenkins) that runs a script which needs to run a command as a specific user.</p>
  
  <p>I tried to do this but it doesn't work:</p>
  
  <pre><code>$secpasswd = ConvertTo-SecureString "myPassword" -AsPlainText -Force
  $mycreds = New-Object System.Management.Automation.PSCredential("DOMAIN\myUsername", $secpasswd)
  
  $Arguments = @()
  $Arguments += "-Command"
  $Arguments += "pwd"
  $Arguments += "&gt;"
  $Arguments += "output.txt"
  Start-Process powershell.exe -ArgumentList $Arguments -Credential $mycreds -NoNewWindow -WorkingDirectory $workingDir
  Start-Sleep 2
  Get-Content "$workingDir\output.txt"
  </code></pre>
  
  <p>I get this output:</p>
  
  <pre><code>Start-Process : This command cannot be executed due to the error: Access is denied.
  At C:\Windows\TEMP\hudson2382859596554223918.ps1:32 char:14
  + Start-Process &lt;&lt;&lt;&lt;  powershell.exe -ArgumentList $Arguments -Credential $mycreds -NoNewWindow -WorkingDirectory $workingDir
      + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
      + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
  </code></pre>
  
  <p>Now if I remove <code>-Credential $mycreds</code> it works fine. The reason why there is that <code>Start-Sleep</code> at the end is that I removed the <code>-Wait</code> after reading <a href="https://stackoverflow.com/q/11874357/831673">this question on SO</a>.</p>
  
  <p>Am I missing something here?</p>