The originally accepted answer for How To Capture Output from Non-elevated Process Run from Elevated Powershell only includes the output of git config
when the BAT file invokes powershell with output redirection (>dummy.txt
). All the other output is captured regardless of whether redirection is applied. Why? Is there a way to capture this output without writing to a dummy file?

- 25,079
- 9
- 80
- 146
1 Answers
Note:
This answer does not address the very specific scenario described / linked to in the question.
Instead, it explains why the caller of
runas.exe
receives no output, becauserunas.exe
itself produces none, and the launched process runs in a new window.Implementing a custom IPC mechanism between the caller and the launched process may be a - complex - option, such as in the answer that prompted your question.
However, this alternative answer shows how you can let the
runas.exe
-launched process itself capture its output, as described below.
A runas.exe
command invariably runs in a new console window, asynchronously from the caller's perspective, and produces no output in the caller's window.
Therefore, the only simple way to capture output from a process launched via runas.exe
is to make that process itself capture its output in a file, such as via a shell's >
redirection; if the target executable isn't a shell, call it via a shell's command-line interface, such as via cmd /c
or powershell -c
Note that you need to call runas.exe
via Start-Process -Wait
if you want the caller to wait until the runas.exe
-launched process has finished running, so you can retrieve and act on its complete output.
Both aspect are demonstrated in this answer.

- 382,024
- 64
- 607
- 775
-
This doesn't explain how all the other output produced by the innermost process *is* captured even without the redirection. The inner process is explicitly capturing all of its output and sending it through a pipe. The question is why is it unable to capture output from one of its steps without the parent runas redirecting it? We don't even need to look at the file to which the output is redirected. Simply redirecting it allows the inner process to capture it. – BlueMonkMN Nov 30 '22 at 12:36
-
@BlueMonkMN, good point, I missed the intricacies of your `System.IO.Pipes.NamedPipeClientStream` approach, which I have yet to take a closer look at. While the following doesn't answer your question, it is worth considering as a pragmatic solution: consider the simpler synchronous approach I've just posted in [this answer](https://stackoverflow.com/a/74632803/45375) to your original question. – mklement0 Nov 30 '22 at 18:50
-
@BlueMonkMN, I've added a disclaimer to the top of this answer, and I think I'll leave it at that. – mklement0 Nov 30 '22 at 19:09