title: Capture EXE output in PowerShell
body: <p>A little background first.</p>
  
  <p>I've been tasked with encrypting files with a Powershell script using GPG (gnupg.org). The specific exe I'm calling is simply gpg.exe. I'd like to capture the output whenever I execute a command.</p>
  
  <p>For instance, I import a public key in powershell as follows:</p>
  
  <pre><code>&amp; $gpgLocation --import "key.txt"
  </code></pre>
  
  <p>$gpgLocation is simply the file location of gpg.exe (default being "C:\Program Files\GNU\GnuPG\gpg.exe"</p>
  
  <p>My entire issue here is that if I try:</p>
  
  <pre><code>&amp; $gpgLocation --import "key.txt" | out-file gpgout.txt
  </code></pre>
  
  <p>All I get is a 1kb file, named appropriately, but it is COMPLETELY blank. I've tried several flags for out-file just to see if I was running into a quirk. </p>
  
  <p>I've also tried sending the command to this code (and capturing the output with the usual out-file etc):</p>
  
  <pre><code>param
  (
      [string] $processname, 
      [string] $arguments
  )
  
  $processStartInfo = New-Object System.Diagnostics.ProcessStartInfo;
  $processStartInfo.FileName = $processname;
  $processStartInfo.WorkingDirectory = (Get-Location).Path;
  if($arguments) { $processStartInfo.Arguments = $arguments }
  $processStartInfo.UseShellExecute = $false;
  $processStartInfo.RedirectStandardOutput = $true;
  
  $process = [System.Diagnostics.Process]::Start($processStartInfo);
  $process.WaitForExit();
  $process.StandardOutput.ReadToEnd();
  </code></pre>
  
  <p>Any ideas? I'm desperate!</p>