3

I ha a function, where I call an application with the & operator. The application produces several line command line output, downloads some files, and returns a string:

& "app.exe" | Out-Host
$var = ...
return $var

It seems, that on the console appears the output produced by app.exe only after app.exe terminates. The user does not have any real time information which file is downloading. Is there a way to continuously update the console when app.exe is running?

robert
  • 3,539
  • 3
  • 35
  • 56

1 Answers1

5

Many console applications buffer theirs output stream, if it known to be redirected. Actually, it is standard behavior of C library. So, buffering is done on app.exe, because of redirection, but not by Out-Host.

A solution would be to not to redirect the output of app.exe, even when the outer command redirected. For than you should know the exact condition when PowerShell does not redirect output stream of console application and, link it directly to their own output stream, which would be a console for interactive PowerShell.exe session. The conditions is:

  1. Command is last item in pipeline.
  2. Command is piped to Out-Default.

Solution would be wrap command into script block, and pipe that script block to Out-Default:

& { & "app.exe" } | Out-Default

The other solution would be to use Start-Process cmdlet with -NoNewWindow and -Wait parameters:

Start-Process "app.exe" -NoNewWindow -Wait
not2qubit
  • 14,531
  • 8
  • 95
  • 135
user4003407
  • 21,204
  • 4
  • 50
  • 60