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.

Invoke-Expression and Invoke-Command: redirecting the error stream to the success stream discards error output

See original GitHub issue

Note 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:open
  • Created 4 years ago
  • Reactions:4
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
onlyanncommented, Jan 12, 2021

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

1reaction
PHeonix25commented, Jan 6, 2021

Just ran into this myself wondering where Write-Error output went!? Thanks for making the issue!

My $PSVersionTable for reference:

Name                           Value
----                           -----
PSVersion                      7.1.0
PSEdition                      Core
GitCommitId                    7.1.0
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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