基本的な使い方

PowerShell
[System.Windows.Forms.MessageBox]::Show(
    "処理が完了しました",
    "完了"
)

ボタンタイプの指定

[System.Windows.Forms.MessageBoxButtons]::Valueで指定する。

説明
OKOKのみ表示
OKCancelOK/Cancel
YesNoYes/No
YesNoCancelYes / No / Cancel
RetryCancelRetry / Cancel

アイコンの指定

アイコンを指定するには[System.Windows.Forms.MessageBox]::Show()メソッドを使います。

PowerShell
[System.Windows.Forms.MessageBox]::Show(
    "メッセージ",
    "タイトル",
    [System.Windows.Forms.MessageBoxButtons]::OK, # ボタンタイプ
    [System.Windows.Forms.MessageBoxIcon]::Error
)

アイコンは以下のような種類があります。

  • Information
  • Warning
  • Error
  • Question

結果の判定

入力結果の判定には[System.Windows.Forms.MessageBoxButtons]のメンバーを使用します。

メンバーは以下のようなものがあり、参照には静的メンバーアクセス演算子::を用います。

  • None
  • OK
  • Cancel
  • Abort
  • Retry
  • Ignore
  • Yes
  • No

使用例は以下のようになります。

PowerShell
$result = [System.Windows.Forms.MessageBox]::Show(
    "削除しますか?",
    "確認",
    [System.Windows.Forms.MessageBoxButtons]::YesNo,
    [System.Windows.Forms.MessageBoxIcon]::Question
)

if ($result -eq [System.Windows.Forms.DialogResult]::Yes) {
    Write-Host "Yes が押されました"
} else {
    Write-Host "No が押されました"
}