Write-Warning
警告メッセージを書き込みます。
構文
Write-Warning [-Message] <string> [<CommonParameters>]
説明
Write-Warning コマンドレットは、警告メッセージを Windows PowerShell ホストに書き込みます。警告への応答は、ユーザーの $WarningPreference 変数の値および WarningAction 共通パラメーターの使用によって異なります。
パラメーター
-Message <string>
警告メッセージを指定します。
必須 |
true |
位置 |
1 |
既定値 |
|
パイプライン入力を許可する |
true (ByValue) |
ワイルドカード文字を許可する |
false |
<CommonParameters>
このコマンドレットは、次の共通パラメーターをサポートします: -Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer、および -OutVariable。詳細については、次を参照してください: about_Commonparameters.
入力と出力
入力値の型は、コマンドレットへのパイプが可能なオブジェクトの型です。戻り値の型は、コマンドレットによって返されるオブジェクトの型です。
入力 |
System.String パイプを使用して警告を含む文字列を Write-Warning に渡すことができます。 |
出力 |
なし Write-Warning は、警告ストリームに対してのみ書き込みを行います。それ以外の出力は生成されません。 |
注
$WarningPreference 変数の既定値は "Continue" です。これは、警告を表示して、コマンドの実行を継続します。$WarningPreference などのユーザー設定変数の有効な値を調べるには、"abc" などのランダムな文字列を設定します。エラー メッセージが表示された場合は、有効な値が一覧表示されます。
例 1
C:\PS>write-warning "This is only a test warning."
説明
-----------
このコマンドは、メッセージ "WARNING: This is only a test warning." を表示します。
例 2
C:\PS>$w = "This is only a test warning."
C:\PS> $w | write-warning
説明
-----------
この例は、パイプライン演算子 (|) を使用して文字列を Write-Warning に渡すことができることを示しています。このコマンドのように、文字列を変数に保存するか、Write-Warning に対して文字列を直接パイプ処理することができます。
例 3
C:\PS>$warningpreference
Continue
C:\PS> write-warning "This is only a test warning."
This is only a test warning.
C:\PS> $warningpreference = "SilentlyContinue"
C:\PS> write-warning "This is only a test warning."
C:\PS>
C:\PS> $warningpreference = "Stop"
C:\PS> write-warning "This is only a test warning."
WARNING: This is only 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 only a test message."
説明
-----------
この例は、$WarningPreference 変数の値が Write-Warning コマンドに与える影響を示しています。
最初のコマンドは $WarningPreference 変数の既定値を表示します。これは "Continue" です。その結果、警告を書き込むと、警告メッセージが表示され、実行が継続されます。
$WarningPreference 変数の値を変更すると、Write-Warning コマンドの結果は再び変更されます。"SilentlyContinue" の値では警告が非表示になります。"Stop" の値では警告が表示され、コマンドの実行が停止します。
$WarningPreference 変数の詳細については、「about_Preference_Variables」を参照してください。
例 4
C:\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"):
説明
-----------
この例は、WarningAction 共通パラメーターが Write-Warning コマンドに与える影響を示しています。任意のコマンドレットと共に WarningAction 共通パラメーターを使用して、Windows PowerShell がそのコマンドから作成される警告に応答する方法を決定できます。WarningAction 共通パラメーターは、その特定のコマンドに対してのみ $WarningPreference の値を上書きします。
このコマンドは Write-Warning コマンドレットを使用して警告を表示します。WarningAction 共通パラメーターの値を "Inquire" に設定すると、コマンドによって警告が表示されたときに、ユーザーに確認メッセージが表示されます。
WarningAction 共通パラメーターの詳細については、「about_CommonParameters」を参照してください。
関連項目
概念
about_Preference_Variables
about_Commonparameters
Write-Debug
Write-Error
Write-Host
Write-Output
Write-Progress
Write-Verbose