Search code examples
powershell

Is there a way to suppress the "extra" output from Write-Error cmdlet?


Looked for this but cannot find a resolution. By "extra" output, I mean the extra text that the Write-Output emits after your error message text. i.e.:

write-error -Message "test"

produces:

write-error -Message "test" : test
+ CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

I would like to only see the text "test", and retrieve it from stderr via a Process object that runs the PS script.

I know a complicated regex (to catch ALL the potential chars that could appear in the category, etc.) could be developed, but I want to avoid that. Don't forget that Write-Error also injects a newline char at every 80th char position due to console, so that would have to be factored in as well.

Is there a way to tell Powershell (2.0) to not be so wordy when writing to stderr and write only the message part?


Solution

  • I think it isn't possible to limit the output of an error but you can get around it using this:

    $Host.UI.WriteErrorLine("My custom error")