title: PowerShell: System.Diagnostics.Process takes forever to run
body: <p>I have a PowerShell scripts, the basic function is to run curl.exe to copy a file from remote ftp server to local.
  At first, I used the following code to run curl.exe:</p>
  <pre><code>$curlPath = &quot;curl.exe&quot;
  $curlArgs = &quot;sftp://download.url.com/filename_20220618.zip --user username:psw -o D:\filename_20220618.zip -k&quot;
  $pinfo = New-Object System.Diagnostics.ProcessStartInfo
  $pinfo.FileName = $curlPath
  $pinfo.Arguments = $curlArgs
  $pinfo.RedirectStandardError = $true
  $pinfo.RedirectStandardOutput = $true
  $pinfo.UseShellExecute = $false
  $pinfo.CreateNoWindow = $true
  $curlProcess = New-Object System.Diagnostics.Process
  $curlProcess.StartInfo = $pinfo
  $curlProcess.Start() | Out-Null
  $curlProcess.WaitForExit()
  </code></pre>
  <p>The code sometimes works. But most of the time will pending forever. I don't known the cause.
  When the code works, I can get error message from $curlProcess.StandError if $curlProcess.ExitCode is not 0.</p>
  <p>To work around, I decided to use</p>
  <pre><code>$curlPath = &quot;curl.exe&quot;
  $curlArgs = &quot;sftp://download.url.com/filename_20220618.zip --user username:psw -o D:\filename_20220618.zip -k&quot;
  $curlProcess = Start-Process $curlPath -ArgumentList $curlArgs -Wait -PassThru -WindowStyle Hidden
  </code></pre>
  <p>The above codes worked. The only problem is when  $curlProcess.ExistCode is not 0, I cannot get the error message. $curlProcess.StandError is always empty.</p>
  <p>Does anyone know how to fix this issue?</p>