Reputation: 34499
If you open up a command prompt and type this:
echo foobar > nul
it will print nothing, since nul
swallows all of its input. But if you run the command with PowerShell:
powershell "echo foobar" > nul
it will output foobar
to the console. Why is this, and what can I do to fix it?
edit: Here is the output of $PSVersionTable
. It looks like I'm using PowerShell v5.0.
Upvotes: 4
Views: 170
Reputation: 437618
Note: I'm assuming you're invoking your command from cmd.exe, not from within PowerShell, which is consistent with the symptoms I'm seeing.
Methinks you've stumbled upon a bug in PS (PowerShell) v5 (not present in v3; comments on the question suggest it's also not in v4), though I don't fully understand why PS is to blame, because I'd expect cmd.exe
to handle the redirection.
I may be missing something, however, so do let me know.
PowerShell should send its so-called success stream - things output by default, including with echo
, which is an alias of Write-Output
- to the outside world's stdout.
In older PS versions >NUL
does effectively suppresses PowerShell's output.
Curiously, the bug in v5 only affects NUL
, whereas redirecting to an actual file works.
As for workarounds:
If your code is v2-compatible, try this:
powershell -version 2 "echo foobar" > NUL
Otherwise, redirect to an actual file and delete that file afterward:
powershell "echo foobar" > "%TEMP%\NUL-bug-workaround" & del "%TEMP%\NUL-bug-workaround"
Upvotes: 5