February 2019
Intermediate to advanced
626 pages
15h 51m
English
The Write-Error command can be used to write non-terminating error messages.
The Write-Error command can be used with nothing more than a message:
Write-Error 'Message'
Or it might include additional information, such as a category and error ID to aid diagnosis by the person using the script:
Write-Error -Message 'Message' -Category 'InvalidOperation' -ErrorId 'UniqueID'
The following example shows a non-terminating error that was raised while running a loop:
function Test-Error {
for ($i = 0; $i -lt 5; $i++) {
Write-Error -Message "Iteration: $i"
}}
Test-Error
The error will be displayed five times without stopping execution.
Setting the value of ErrorAction to Stop will cause Write-Error to throw a terminating error, ending ...
Read now
Unlock full access