Write-Error

Verb: WriteChapter 18: Handle errors and finish

Writes an error message to the error stream, without throwing a terminating error by default.

Syntax

Short syntax for this command. The parameter table below may list more parameters than this short line shows.

Syntax
Write-Error -Message <Text>

Parameters

ParameterWhat it doesIn short syntax
-Messagethe error text to writeYes

Examples

Reports the missing input file. By default the script keeps running after Write-Error, so pair it with a clear check or a return when the step cannot continue.

Example 1
if (-not (Test-Path -Path "C:\Temp\input.csv")) { Write-Error -Message "Input file is missing." }

Illustrative output: see the lesson example for this command.

Common mistakes

General cautions for this command.

  • Guessing parameters instead of checking Get-Help for this command.
  • Running a changing form of this command before previewing its effect.

Related commands

Commands that share this command's lesson come first, then commands that share its verb. Every link goes to a real command page.

Related lessons