title: Getting stdout to appear on console with PowerShell Invoke-Command and Process
body: <p>I've got a Powershell script that is making use of Invoke-Command.  Here's the code that is being invoked:</p>
  
  <pre><code>$scriptblock = {
    $process = New-Object system.Diagnostics.Process
  
    $si = New-Object System.Diagnostics.ProcessStartInfo
    $si.FileName = $cmd
    $si.Arguments = $cmd_args
    $si.UseShellExecute = false
    $si.RedirectStandardOutput = true
  
    $process.StartInfo = $si
    $process.Start()
    $processId = $process.Id
    $process.WaitForExit()
  }
  
  Invoke-Command -ScriptBlock $scriptblock
  </code></pre>
  
  <p>The issue is related to getting the process's standard output to display to the PowerShell console, when the script is run from the PowerShell window.</p>
  
  <p>Now, I am certain that the executable $cmd emits to stdout as when I run the executable with cmd.exe it shows output to that console.  I've also been able to successfully redirect the output when I call this Powershell script from a NodeJS script like so:</p>
  
  <pre><code>var exec  = require('child_process').exec,
  var cmd   = 'powershell -ExecutionPolicy Bypass "' + launch_cmd + " \'" + ver + "\'\"";
  
  console.log("\n\tUsing command for NodeJS child process:\n\t\t" + cmd + "\n\n")
  child = exec(cmd, { cwd : prj }, function(stdout, stderr){});
  
  child.stdout.on("data",function(data){
      process.stdout.write(data);
  });
  
  child.stderr.on("data",function(data){
      process.stderr.write(data);
  });
  
  child.on("exit",function(){
      console.log("\n\nFinished Process");
  });
  </code></pre>
  
  <p>I want to be able to get the StandardOutput to the PowerShell console.  I  reviewed <a href="https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.110).aspx" rel="nofollow">this TechNet documentation</a> and tried the following in the script block, but to no avail:</p>
  
  <pre><code>$scriptblock = {
    $process = New-Object system.Diagnostics.Process
  
    $si = New-Object System.Diagnostics.ProcessStartInfo
    $si.FileName = $cmd
    $si.Arguments = $cmd_args
    $si.UseShellExecute = false
    $si.RedirectStandardOutput = true
  
    $process.StartInfo = $si
    $process.Start()
    $process.BeginOutputReadLine()
    $process.StandardOutput.ReadToEnd()
    $processId = $process.Id
    $process.StandardOutput
    $process.WaitForExit()
  }
  
  Invoke-Command -ScriptBlock $scriptblock
  </code></pre>
  
  <p>Any suggestions as to how I can make the process display its standard output to the PowerShell console?</p>