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?
Use the try block to define a section of a script in which you want PowerShell to monitor for errors. When an error occurs within the try block, the error is first saved to the $Error automatic variable. PowerShell then searches for a catch block to handle the error.
An error in PowerShell is a message that's displayed on screen when something goes wrong. By default, PowerShell displays its errors in red text on a black background (in the console host, that is; the Integrated Scripting Environment (ISE) uses red text).
The echo command is used to print the variables or strings on the console. The echo command has an alias named “Write-Output” in Windows PowerShell Scripting language. In PowerShell, you can use “echo” and “Write-Output,” which will provide the same output.
I think isn't possible limit the output of an error but you can workaround like this:
$Host.UI.WriteErrorLine("My custom error")
$Host.UI.WriteErrorLine doesn't update the $error variable:
PS> $error.Clear()
PS> $Host.UI.WriteErrorLine("My custom error")
My custom error
PS> $Error.Count
0
You should use the Write-Error
cmdlet but the output is still verbose:
PS> Write-Error "My custom error"
Write-Error "My custom error" : My custom error
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
There's an option to output less verbose message with the built-in $ErrorView variable, its default value is 'Normal' (verbose). You can set it to "CategoryView" for brief messages.
PS> $ErrorView="CategoryView"
PS> Write-Error "My custom error"
NotSpecified: (:) [Write-Error], WriteErrorException
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With