0

It was my understanding that anything that writes to $error should be re-directable to $null, but I can't make it work, even with something as simple as substring or a bad cmdlet name:

sample screenshot

PS C:\WINDOWS\system32> asdf 2>$null asdf : The term 'asdf' is not recognized as the name of a cmdlet... 

All of the examples here do not seem to work: Redirecting output to $null in PowerShell, but ensuring the variable remains set

PS C:\WINDOWS\system32> $qwerty = "a" PS C:\WINDOWS\system32> $qwerty.substring(0,2) 2>$NULL Exception calling "Substring" with "2" argument(s): "Index and length... PS C:\WINDOWS\system32> $qwerty.substring(0,2) | out-null Exception calling "Substring" with "2" argument(s): "Index and length... 
4
  • I'm not sure but I think that it might work but you need to pass it to an existing program as a parameter, otherwise it assumes that the executable is named "asdf" and not the variables of "asdf" being passed to it. Such as you are trying to run asdf.exe so it just fails .. and you want it to run something like dir /asdf or something like that for this to work. Now forgive me if I am wrong but I think this should at least get you onto the right track. Commented Jul 25, 2022 at 18:07
  • It's the powershell response that I'm trying to redirect out to null, rather than the target process or cmdlet. I'll give another example with a powershell function: $blah.substring(0,2) 2>$null Exception calling "Substring" with "2" argument(s): "Index and length must... etc. The error message is still outputting to the console rather than to the $NULL variable Commented Jul 25, 2022 at 18:21
  • get-command asdf 2>$null Commented Jul 25, 2022 at 18:30
  • get-command $blah.substring(0,2) 2>$null Exception calling "Substring" with "2" argument(s): "Index and length... Didn't work Commented Jul 25, 2022 at 18:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.