How to capture error output only in a variable in PowerShell How to capture error output only in a variable in PowerShell powershell powershell

How to capture error output only in a variable in PowerShell


You can call the command a slightly different way and use the -ErrorVariable parameter in PowerShell:

Invoke-Expression "$command $params" -ErrorVariable badoutput

$badoutput will now contain the contents of the error string.


To add to arco444, the specific exception can be obtained by using $badoutput[0].Exception.

-ErrorAction and -ErrorVariable has more information on to effectively use the ErrorAction and ErrorVariable parameters built into PowerShell.

Here is the easiest way to show this working:

PS> Stop-Process 13,23Stop-Process : Cannot find a process with the process identifier 13.At line:1 char:13+ Stop-Process  <<<< 13,23Stop-Process : Cannot find a process with the process identifier 23.At line:1 char:13+ Stop-Process  <<<< 13,23PS> Stop-Process 13,23 -ErrorAction Stop  # Only 1 errorStop-Process : Command execution stopped because the shell variable “ErrorActionPreference” is set to Stop: Cannot find a process with the process identifier 13.At line:1 char:13+ Stop-Process  <<<< 13,23 -ErrorAction Stop  # Only 1 errorPS> Stop-Process 13,23 -ErrorAction silentlycontinue  # No errorsPS> Stop-Process 13,23 -ErrorAction inquire  # ASKConfirmCannot find a process with the process identifier 13.[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help(default is “Y”):hStop-Process : Command execution stopped because the user selected the Halt option.At line:1 char:13+ Stop-Process  <<<< 13,23 -ErrorAction inquire  # ASKPS>PS>PS> Stop-Process 13,23 -ErrorVariable a -ErrorAction SilentlyContinuePS> $a[0]Stop-Process : Cannot find a process with the process identifier 13.At line:1 char:13+ Stop-Process  <<<< 13,23 -ErrorVariable a -ErrorAction SilentlyContinuePS> $a[0] |fl * -ForceException             : Microsoft.PowerShell.Commands.ProcessCommandExcepti                        on: Cannot find a process with the process identifi                        er 13.TargetObject          : 13CategoryInfo          : ObjectNotFound: (13:Int32) [Stop-Process], ProcessC                        ommandExceptionFullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Comma                        nds.StopProcessCommandErrorDetails          :InvocationInfo        : System.Management.Automation.InvocationInfoPS> $a |ft TargetObject -force -autoTargetObject————          13          23

Now one thing that is not obvious to people is that you can specify a “+” in front of the variable name for ErrorVariable and we will ADD the errors to that variable.

PS> $err=@()PS> stop-process 13 -ea silentlycontinue -ErrorVariable errPS> $err.count1PS> stop-process 23 -ea silentlycontinue -ErrorVariable +errPS> $err.count2PS> $errStop-Process : Cannot find a process with the process identifier 13.At line:1 char:13+ stop-process  <<<< 13 -ea silentlycontinue -ErrorVariable errStop-Process : Cannot find a process with the process identifier 23.At line:1 char:13+ stop-process  <<<< 23 -ea silentlycontinue -ErrorVariable +err

Lastly, you don’t need to type out –ErrorAction or –ErrorVariable, we have defined parameter aliases for these so you can just type –EA and -EV.


Here is simpler solution by Simon Wahlin using sub expressions

$output = & $command $params 2>&1

Would be:

$errOutput = $( $output = & $command $params ) 2>&1