Powershell: How to show a message box

Sometimes while a powershell script is running you want to show a MessageBox with a information or warning to the user. In Windows Powershell no Commandlet exists to show a Message Box.

Nevertheless it is possible by using the .NET Windows.Forms.MessageBox class:-).

First of all load the assembly.

# Load assembly
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

To show the messagebox call the static function show(“Message Text”)

$oReturn=[System.Windows.Forms.Messagebox]::Show("This is the Message text")

The function returned a value of enum System.Windows.Forms.DialogResult, that indicates which Button was pressed.
Possible Returncodes, depending on which button was pressed, are:

[system.enum]::getValues($oReturn.GetType())
None
OK
Cancel
Abort
Retry
Ignore
Yes
No

The default Button is the OK Button, there are further 5 combinations, see below.

[system.enum]::getNames([System.Windows.Forms.MessageBoxButtons])|foreach{[console]::Writeline("{0,20} {1,-40:D}",$_,[System.Windows.Forms.MessageBoxButtons]::$_.value__)}
                  OK 0
            OKCancel 1
    AbortRetryIgnore 2
         YesNoCancel 3
               YesNo 4
         RetryCancel 5

An Example, a Message box with an Ok and a Cancel button and a check which button was pressed:

$oReturn=[System.Windows.Forms.MessageBox]::Show("Message Text","Title",[System.Windows.Forms.MessageBoxButtons]::OKCancel)	
switch ($oReturn){
	"OK" {
		write-host "You pressed OK"
		# Enter some code
	} 
	"Cancel" {
		write-host "You pressed Cancel"
		# Enter some code
	} 
}

[System.Windows.Forms.MessageBoxButtons]::OKCancel
[System.Windows.Forms.MessageBoxButtons]::OKCancel
Some examples

[System.Windows.Forms.MessageBoxButtons]::AbortRetryIgnore
[System.Windows.Forms.MessageBoxButtons]::AbortRetryIgnore
[System.Windows.Forms.MessageBoxButtons]::YesNoCancel
[System.Windows.Forms.MessageBoxButtons]::YesNoCancel
you can also use the number instead of the numeric constants to specify the buttons

[System.Windows.Forms.MessageBox]::Show("Message Text","Title",1)

This oneliner shows all possible Button combinations consecutively

[system.enum]::getValues([System.Windows.Forms.MessageBoxButtons])|foreach {[System.Windows.Forms.MessageBox]::Show("["+$_.GetType()+"]::"+$_.ToString(),"Message box Buttons",$_)}

You can style the message box with an icon, 4 are available

[system.enum]::getNames([System.Windows.Forms.MessageBoxIcon])|foreach{[console]::Writeline("{0,20} {1,-40:D}",$_,[System.Windows.Forms.MessageBoxIcon]::$_.value__)}
                None 0
                Hand 16
               Error 16
                Stop 16
            Question 32
         Exclamation 48
             Warning 48
            Asterisk 64
         Information 64

Example:

[System.Windows.Forms.MessageBox]::Show("Message Text","Title",[System.Windows.Forms.MessageBoxButtons]::OKCancel,[System.Windows.Forms.MessageBoxIcon]::Warning)

[System.Windows.Forms.MessageBoxIcon]::Warning
[System.Windows.Forms.MessageBoxIcon]::Warning
Same with numbers instead of numeric constants

[System.Windows.Forms.MessageBox]::Show("Message Text","Title",1,48)

the remaining…

[System.Windows.Forms.MessageBoxIcon]::Question
[System.Windows.Forms.MessageBoxIcon]::Question
[System.Windows.Forms.MessageBoxIcon]::Hand, Stop or Error
[System.Windows.Forms.MessageBoxIcon]::Hand, Stop or Error
[System.Windows.Forms.MessageBoxIcon]::Asterisk or Information
[System.Windows.Forms.MessageBoxIcon]::Asterisk or Information
 

All available icons consecutively

[system.enum]::getValues([System.Windows.Forms.MessageBoxIcon])|foreach {[System.Windows.Forms.Messagebox]::Show("["+$_.GetType()+"]::"+$_.ToString(),"Message box Icons",[System.Windows.Forms.MessageBoxButtons]::OK,$_)}

Michael

7 thoughts on “Powershell: How to show a message box”

    1. Months later, but @Scott Fisher, the Show method accepts some additional parameters, you can find by searching Google for “MessageBox.Show Method”. Here’s an example prototype:
      Show(Message, Title, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions)

  1. i want to see the current time in a messagebox. i have sheduled the script. it is also displaying the powershell prompt window along with the message box. how to hide it? Thanks in advance for valuable suggestions

    1. Hi Fanatsys,

      like this?

      powershell -WindowStyle Hidden "[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms');[System.Windows.Forms.Messagebox]::Show('Current Time:' + (Get-Date).ToLongTimeString());"
      

      Michael

Leave a Reply