Invoke-Expression and Invoke-Command: redirecting the error stream to the success stream discards error output
See original GitHub issueNote that the symptom only occurs when redirecting to another stream, either via 2>&1
or *>&1
.
By contrast, redirecting to a file works as expected.
Steps to reproduce
Run the following Pester test:
# Create a string with a command that writes to all streams.
$cmd = '1; write-error 2; write-warning 3; write-verbose -vb 4; write-debug -debug 5; write-information -infa continue 6'
# Make sure that all streams can be directed to the success output stream (1).
(Invoke-Expression $cmd *>&1).Count | Should -Be 6
Note: the same applies to (at least local use of) Invoke-Command
:
(Invoke-Command ([scriptblock]::create($cmd)) *>&1).Count | Should -Be 6
The workaround is enclosure in (...)
, but it comes at the expense of losing streaming behavior.
( (Invoke-Expression $cmd) *>&1).Count | Should -Be 6
Expected behavior
The test should pass - all 6 streams should show up in the success output stream.
Actual behavior
Expected 6, but got 5.
That is, the error output was quietly discarded, as you 'll see if you run the command inside (...)
in isolation
Environment data
PowerShell Core 7.0.0-preview.3
Windows PowerShell v5.1.18362.145 on Microsoft Windows 10 Pro (64-bit; Version 1903, OS Build: 18362.295)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Stderr and stdout for powershell with Invoke-Expression ...
Stderr output is passed through (and is not reflected in $Error ), but you can redirect it to a file with 2>$file ,...
Read more >Avoid using Invoke-Expression - PowerShell
When a string from an untrusted source such as user input is passed directly to Invoke-Expression , arbitrary commands can be executed. Always ......
Read more >Discarding Output - powershell.one
By redirecting stream 6 to null, all outputs from Write-Host and Write-Information are discarded. You can append 6>$null to any command or ...
Read more >about Redirection - PowerShell
Use the Out-File cmdlet, which sends command output to a text file. ... It uses 2>&1 to redirect the Error stream to the...
Read more >Redirection - PowerShell - SS64.com
When redirection is performed without specifying a numeric handle, the default is 1, i.e. '>' will not redirect error messages. In PowerShell 5.0+...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
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
@mklement0 Somehow, it wasn’t working in a PowerShell Core console but was fine on Windows PowerShell. Now that I try to reproduce, it works in both. I must have been doing something stupid… Thank you @mklement0 for your help!
Just ran into this myself wondering where
Write-Error
output went!? Thanks for making the issue!My $PSVersionTable for reference: