0

I want a PowerShell one-liner that acts just like

[MyType]$var = My-Cmdlet -MyParam MyArg

...but suppresses stderr.

No, the following does not work:

([MyType]$var = My-Cmdlet -MyParam MyArg) 2>&1>$null

...as evidenced by the following example:

PS C:\> ($var = Get-installedModule "hooha") 2>&1>$null
PS C:\> $var.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     ErrorRecord                              System.Object

PS C:\> $var
Get-Package: No match was found for the specified search criteria and module names 'hooha'.

PS C:\> $var = Get-installedModule "hooha"
Get-Package: No match was found for the specified search criteria and module names 'hooha'.

PS C:\> $var.GetType()
InvalidOperation: You cannot call a method on a null-valued expression.
PS C:\>

Edit, for people who marked this as a duplicate of a not-equivalent question, and have left somewhat irrelevant comments: No, this is not the same question as Powershell: How can I stop errors from being displayed in a script? . I am trying to suppress both stderr and stdout for a command, and assign its returned value to a variable, in the same command line, and have the assignment behave the same way as an uncomplicated assignment including in the error case -- i.e. assign $null if the command fails.

Keith Russell
  • 560
  • 4
  • 12
  • When your query `$var = Get-installedModule "hooha"` does not return any result your variable `$var` is just empty. – Olaf Jan 17 '23 at 00:37
  • @Olaf I know. But it's *not* empty when I suppress stderr and stdout. That's the problem. – Keith Russell Jan 17 '23 at 00:41
  • 1
    I'm actually unsure what your question is. If you want to suppress errors you may use `-ea 0` or `-ErrorAction SilentlyContinue` – Olaf Jan 17 '23 at 00:45
  • @Olaf In my example, suppression (redirection) changes the behavior of the assignment. I want to suppress but have the assignment behave just as if I didn't suppress. i.e. I want the assignment to assign `$null` if the command fails. – Keith Russell Jan 17 '23 at 00:50
  • @Olaf `-ea 0` did it! Thanks! If you write that as the answer, I'll accept it so that you can get the points. – Keith Russell Jan 17 '23 at 00:53
  • The full solution is `($var = MyCmd -ErrorAction SilentlyContinue) 1>$null`. In my case, however, I didn't care about suppressing stdout, but I didn't say this in my question. – Keith Russell Jan 17 '23 at 01:00
  • @KeithRussell, an assignment by itself never produces success output, it only does if you enclose it in `(...)`. Thus, simply don't enclose it in `(...)`; if you want to use an assignment to simply _discard_ success output, use `$null = ...`. To completely suppress _non-terminating_ errors from cmdlets and advanced functions / scripts, use `-ErrorAction Ignore` (`-ErrorAction SilentlyContinue` suppresses error _display_, but still records the error in the session-wide automatic `$Error` variable). To also suppress _terminating_ errors, more effort is needed. – mklement0 Jan 17 '23 at 02:44
  • @KeithRussell, an alternative to `-ErrorAction SilentlyContinue` that works with _any_ command's non-terminating error output, including simple function/scripts and external programs is `2>$null`. – mklement0 Jan 17 '23 at 02:45
  • You can use `| Out-Null` at end of any command-line to suppress the output. – SavindraSingh Jan 17 '23 at 08:25
  • @savindraSingh `| Out-Null` only suppresses stdout, not stderr. But this is irrelevant, because in my example I already suppress stdout and stderr. Suppression alone is not my question. And `| Out-Null` *does* still have the problem I'm asking about: How to suppress without changing the behavior of assignment in the error case? – Keith Russell Jan 17 '23 at 17:18

0 Answers0