Start-Process: allow redirecting std output/error to console stream
See original GitHub issueSummary 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
Issue Analytics
- State:
- Created a year ago
- Comments:20 (7 by maintainers)
Top Results From Across the Web
Capturing standard out and error with Start-Process
Is there a bug in PowerShell's Start-Process command when accessing the StandardError and StandardOutput properties? If I run the following I ...
Read more >Start-Process -RedirectStandardOutput issues
I am trying to redirect the output and error separately to two different files when i use start-process to run PostgreSQL base backup....
Read more >Output redirection fails when using Start-Process to launch ...
This cmdlet sends the output generated by the process to a file that you specify. ... Start the new process in the current...
Read more >Can I redirect output to a log file and background a process ...
One problem with your first command is that you redirect stderr to where stdout is (if you changed the $ to a &...
Read more >How to redirect shell command output | Enable Sysadmin
How to redirect shell output · 1. Redirect STDOUT · 2. Redirect STDERR · 3. Send STDOUT and STDERR to the same file...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.