Write-Warning

Write a warning string (3) in reverse video to the display

Syntax
      Write-Warning [-message] string [CommonParameters]

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

The -WarningAction common parameter may be used to supress the appearance of warnings.
When -WarningAction is not specified, the default is determined by the preference variable.

The user's $WarningPreference variable determines whether the message you provide to Write-Warning is displayed at the expected point in a script's operation. The default value of this variable is Continue, so by default, warning messages are shown.

Warning Sound/Beep

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

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

Examples

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.
Confirm
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-Information - Write to the information data stream (6).
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 © 1999-2024 SS64.com
Some rights reserved