Write a warning message to the host display.

      Write-Warning [-message] string [CommonParameters]

   -message string
       The warning message to send to the host.
       {may be piped}

       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
       -OutBuffer, PipelineVariable, -OutVariable.

The -WarningAction common parameter may be used to supress the appearance of warnings.
When -WarningAction is not specified, the default is determined by the user's $WarningPreference variable, which by default will display the warning text and continue.

Write-Warning (and Write-host) write only to the host/screen, so they will be visible even if the output is redirected to file.
If you need the message string to be passed to the success pipeline, use Write-Output instead.

Warning Sound/Beep

You can also play an Alert Sound/Beep with [console]::Beep (via Idera)

$frequency = 800
$durationMS = 2000
[console]::Beep($frequency, $durationMS)


Write a warning message:

PS C:\> Write-Warning "The quick brown fox."
The quick brown fox.

Pipe a string to Write-Warning:

PS C:\> $myString = "This is a test warning."
PS C:\> $myString | Write-Warning
This is a test warning.

Set the $WarningPreference variable so that Write-Warning will stop execution:

PS C:\> $warningpreference = "Stop"
PS C:\> Write-Warning "This is a test warning."
WARNING: This is a test message. 
Write-Warning : Command execution stopped because the shell variable "WarningPreference" is set to Stop. 
At line:1 char:14
     + Write-Warning <<<<  "This is a test message."

Show the effect of the -WarningAction common parameter on Write-Warning:

PS C:\> Write-Warning "This is a test warning." -WarningAction Inquire
WARNING: This is a test warning. 
Continue with this operation? 
 [Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"):

“History is a vast early warning system” ~ Norman Cousins

Related PowerShell Cmdlets:

Write-Debug - Write a debug message to the host display.
Write-Error - Write an object to the error pipeline.
Write-Host - Display objects through the host user interface.
Write-Output - Write an object to the pipeline.
Write-Progress - Display a progress bar.
Write-Verbose - Write a string to the host’s verbose display.

Copyright © SS64.com 1999-2019
Some rights reserved