Redirecting output to $null in PowerShell, but ensuring the variable remains set

I’d prefer this way to redirect standard output (native PowerShell)…

($foo = someFunction) | out-null

But this works too:

($foo = someFunction) > $null

To redirect just standard error after defining $foo with result of “someFunction”, do

($foo = someFunction) 2> $null

This is effectively the same as mentioned above.

Or to redirect any standard error messages from “someFunction” and then defining $foo with the result:

$foo = (someFunction 2> $null)

To redirect both you have a few options:

2>&1>$null
2>&1 | out-null

ADDENDUM:

Please note that (Windows) powershell has many more streams than a linux based OS. Here’s the list from MS docs:

enter image description here

Thus you can redirect all streams using the wildcard with *>$null, and you can also use a file instead of $null.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)