MessageBox (.Net Method)

Display a message box to the user, optionally with a choice of: OK/Cancel/Yes/No.

      Add-Type -AssemblyName PresentationCore,PresentationFramework
      $Result = [System.Windows.MessageBox]::Show(MessageBody,Title,ButtonType,Image)
       A string containing the message text.

       A string containing the title bar text for the message box.

      An integer containing the type of button to display:

      An integer representing the icon to display (if any).
Image Value   ButtonType Value
None 0   OK 0
Hand / Error / Stop 16   OKCancel 1
Question 32   YesNoCancel 3
Warning / Exclamation 48   YesNo 4
Asterisk / Information 64      

The Button type and Image can be supplied as either a number, the equivalent text string or with a full reference like: [System.Windows.MessageBoxButton]::YesNoCancel

Sample image icons can be viewed on the MessageBoxImage Enum -

The MessageBox will return one of these values:

Result Value
None (no result) 0
OK 1
Cancel 2
Yes 6
No. 7

All the values given above can be enumerated using getNames()



Display a simple message to the user:

Add-Type -AssemblyName PresentationCore,PresentationFramework
$msgBody = "This is a simple message with just the default OK button"

Ask the user a question and save the answer (Yes, No or Cancel) in the variable $Result:

Add-Type -AssemblyName PresentationCore,PresentationFramework

$msgBody = "Reboot the computer now?"
$msgTitle = "Confirm Reboot"

$msgButton = 'YesNoCancel'
$msgImage = 'Question'
$Result = [System.Windows.MessageBox]::Show($msgBody,$msgTitle,$msgButton,$msgImage)
Write-Host "The user chose: $Result [" ($result).value__ "]"

“To Thales the primary question was not what do we know, but how do we know it” ~ Aristotle

Related PowerShell Cmdlets

Read-Host - Read a line of input from the host console.
Write-Host - Display objects through the host user interface.

Localizing System MessageBox - Multi language Message Box.
An alternative but similar MessageBox method is available via Windows Forms, loading [System.Windows.Forms.MessageBox]
Equivalent CMD script: CHOICE
Equivalent VBScript: MSGBOX
Equivalent bash command (Linux): case / select - Accept keyboard input.

Copyright © 1999-2024
Some rights reserved