question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Start-Process: allow redirecting std output/error to console stream

See original GitHub issue

Summary of the new feature / enhancement

Hello. I am trying to start a process in a new window with Start-Process and i’m -waiting 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 file
  • get-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:open
  • Created a year ago
  • Comments:20 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jborean93commented, Sep 13, 2022

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.

1reaction
aetonsicommented, Sep 6, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found