PowerShell: Start-Process: allow redirecting std output/error to console stream
Summary of the new feature / enhancement
Hello.
I am trying to start a process in a new window with Start-Process
and i’m -wait
ing for its execution.
Then i need to process its output. I would like to do it all in memory, without writing to disk, as the output is confidential. Unfortunately at the moment this doesn’t seem to be possible (correct me if i’m wrong) without resorting to .NET implementations like this…
Currently, this is the best you can do:
$output = & {start-process powershell "read-host 'Enter string: '" -Wait -RedirectStandardOutput output.txt ; get-content output.txt}
In this example,
powershell "read-host 'Enter string: '"
is my command with confidential output.-Wait
waits for my command to exit-RedirectStandardOutput
permits the redirection to a fileget-content
simply reads back that file.
Proposed technical implementation details (optional)
A possible solution would be a parameter like -RedirectStandardOutputToStream
/ -RedirectStandardErrorToStream
$output = & {start-process powershell "read-host 'Enter string: '" -wait -RedirectStandardOutputToStream 1}
Where 1
is the ID of the success stream.
Or, more simply:
$output = & {start-process powershell "read-host 'Enter string: '" -wait -GetStandardOutput}
… with the corresponding -GetStandardError
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 20 (8 by maintainers)
Note that there’s a previous discussion about generally allowing targeting variables in contexts where file paths are expected, so that
Start-Process ... -RedirectStandardOutput variable:out
would capture stdout in variable$out
.This is not only more flexible than
-RedirectStandardOutput 1
, it also prevents the output from instantly mixing with the caller’s streams, which in asynchronousStart-Process
calls (which are typical) you wouldn’t want.See:
>
)Start-Process
)Redirecting to variables would mean that the process would have to completely finish before you printed to console. Being able to redirect to the current runspace’s streams would mean you could have streaming output.
That doesn’t really help here for Start-Process if I understand it correctly? You can already capture output with just invoking the exe today. Supporting Start-Process redirection to a var would be quite tricky unless it meant -Wait was always implicit as it would require a background task to be running which could mutate the var as it’s being read.
@mklement0 Mmh i see, that would be cool. Not the same as what i was picturing (
-RedirectStandardOutputToStream 1
would print to the success stream, and you could capture it in a variable)… but ultimately that’s what i would like to end up to do: save the output to a variable to reuse it.What are you using start-process at all ?
$output = powershell "read-host 'I should not ask for parameters to be typed' "
or$output = powershell "read host 'hyphen missing will cause error' " 2> null
if you want to remove error output.If it is a command line program start-process will (by default) run in a new window, not receive output and not wait for the program it has started to finish. You can specify Wait or NoNewWindow but you still need to work around the output not coming back
$output = Invoke-Command -ScriptBlock {powershell "read-host 'I should not ask for parameters to be typed' " }
Also works the way you want, I think.