다음을 통해 공유


Write-Warning

Writes a warning message.

구문

Default (기본값)

Write-Warning
    [-Message] <String>
    [<CommonParameters>]

Description

The Write-Warning cmdlet writes a warning message to the PowerShell host. The response to the warning depends on the value of the user's $WarningPreference variable and the use of the WarningAction common parameter.

예제

Example 1: Write a warning message

This command displays the message "WARNING: This is only a test warning."

Write-Warning "This is only a test warning."

Example 2: Pass a string to Write-Warning

This command shows that you can use a pipeline operator (|) to send a string to Write-Warning. You can save the string in a variable, as shown in this command, or pipe the string directly to Write-Warning.

$w = "This is only a test warning."
$w | Write-Warning

Example 3: Set the $WarningPreference variable and write a warning

This example shows the effect of the value of the $WarningPreference variable on a Write-Warning command.

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

The first command displays the default value of the $WarningPreference variable, which is Continue. As a result, when you write a warning, the warning message is displayed and execution continues.

When you change the value of the $WarningPreference variable, the effect of the Write-Warning command changes again. A value of SilentlyContinue suppresses the warning. A value of Stop displays the warning and then stops execution of the command.

For more information about the $WarningPreference variable, see about_Preference_Variables.

Example 4: Set the WarningAction parameter and write a warning

This example shows the effect of the WarningAction common parameter on a Write-Warning command. You can use the WarningAction common parameter with any cmdlet to determine how PowerShell responds to warnings resulting from that command. The WarningAction common parameter overrides the value of the $WarningPreference only for that particular command.

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

This command uses the Write-Warning cmdlet to display a warning. The WarningAction common parameter with a value of Inquire directs the system to prompt the user when the command displays a warning.

For more information about the WarningAction common parameter, see about_CommonParameters.

매개 변수

-Message

Specifies the warning message.

매개 변수 속성

형식:String
Default value:None
와일드카드 지원:False
DontShow:False
별칭:Msg

매개 변수 집합

(All)
Position:0
필수:True
파이프라인의 값:True
속성 이름별 파이프라인의 값:False
나머지 인수의 값:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

입력

String

You can pipe a string that contains the warning to this cmdlet.

출력

None

This cmdlet returns no output. It writes only to the warning stream.

참고

The default value for the $WarningPreference variable is Continue, which displays the warning and then continues executing the command. To determine valid values for a preference variable such as $WarningPreference, set it to a string of random characters, such as "abc". The resulting error message lists the valid values.