Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to error stream in Powershell using Write-Error

Why isn't Powershell's Write-Error cmdlet working for me? My output doesn't look like the examples in the documentation:

PS C:\> Write-Error "This is an error"
Write-Error "This is an error" : This is an error
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

I had been expecting output similar to Write-Warning:

PS H:\> Write-Warning "This is a warning"
WARNING: This is a warning

From the Write-Error and about_preference_variables documentation I thought I should not see any exceptions?

PS H:\> Get-Help About_Preference_Variables

$ErrorActionPreference
----------------------

...

        PS> $erroractionpreference                      
        Continue        # Display the value of the preference.                

        PS> write-error "Hello, World"                  
                                # Generate a non-terminating error.

        write-error "Hello, World" : Hello, World       
                                # The error message is displayed and
                                  execution continues.

        PS> write-error "Hello, World" -ErrorAction:SilentlyContinue
                                # Use the ErrorAction parameter with a 
                                  value of "SilentlyContinue".
        PS>                                             
                                # The error message is not displayed and
                                  execution continues.
like image 420
sourcenouveau Avatar asked Mar 10 '11 21:03

sourcenouveau


People also ask

How do you Write error messages in PowerShell?

To write a non-terminating error, enter an error message string, an ErrorRecord object, or an Exception object. Use the other parameters of Write-Error to populate the error record. Non-terminating errors write an error to the error stream, but they do not stop command processing.

How do you Write to stderr in PowerShell?

To write to the outside world's stderr by default, use [Console]::Error. WriteLine() , as recommended in the answer, at the expense of being able to capture such output inside a PowerShell session.

What is the difference between Write error and throw in PowerShell?

The main difference between the Write-Error cmdlet and the throw keyword in PowerShell is that the former simply prints some text to the standard error stream (stderr), while the latter actually terminates the processing of the running command or function, which is then handled by PowerShell by sending out information ...


2 Answers

To get output similar to write-warning, you can do this:

$Host.UI.WriteErrorLine("This is an error")

(props to Chris Sears for this answer)

like image 159
Neil Avatar answered Sep 22 '22 09:09

Neil


Why don't you think it is working? Keep in mind that PowerShell distinguishes between non-terminating errors like the one above and terminating errors like you get when you execute throw 'Access denied.'. Non terminating errors are written to stderr and logged in the $error collection but they don't stop processing of a script. This feature is extremely handy when you are processing (say deleting or copying) a bunch of files. You want to know which files couldn't be processed but you don't want the entire operation to stop on the first file that errors out.

PowerShell also gives you the option to "convert" non-terminating errors to terminating errors e.g.

Remove-Item c:\file-doesnt-exist -ErrorAction Stop; "Did I get here"

Notice in this case, execution stops and doesn't print out the string at the end. Try it without the -ErrorAction Stop and you will see the error but you will also see the string "Did I get here".

If you want to control the Catogory info you can use the -Category parameter like so:

PS> write-error "foo" -Category 'InvalidResult'
write-error "foo" -Category 'InvalidResult' : foo
    + CategoryInfo          : InvalidResult: (:) [Write-Error], WriteErrorExce..
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

But the WriteErrorException is the mechanism (I think) by which this cmdlet raises the error. There is an Exception parameter but I've not had much luck using it.

like image 29
Keith Hill Avatar answered Sep 22 '22 09:09

Keith Hill