Thursday, October 9, 2014

#28 : Display Messagebox with Powershell

Although, I never came accross such a situation where I need to throw a messagebox. But, just as I was playing, I tried to throw messagebox from Powershell.

Generating a Messagebox -

1. Load the Assembly



GAC Version Location
--- ------- --------
True v2.0.50727 C:\Windows\assembly\GAC_MSIL\System.Windows.Forms\\System.Windows.Forms.dll

Note: If you don't want the output, you can simple redirect to Out-nul. This will skip displaying assembly loading statement.

2. Display a simple Messagebox
[System.Windows.Forms.MessageBox]::Show("We are proceeding with next step.")

Now, the messagebox appears something like this -

If you see above message, you will find Title is missing. Let's add a title also by adding below piece of code -
[System.Windows.Forms.MessageBox]::Show("We are proceeding with next step." , "Status")

So, this was all about showing message with title. This was just OK message so, there is nothing to decide for user except pressing OK button.
Types of Messageboxes :
We have 6 types of Messageboxes in Powershell -

0: OK
1: OK Cancel
2: Abort Retry Ignore
3: Yes No Cancel
4: Yes No
5: Retry Cancel

Note: The number mentioned in left is the third parameter of Messagebox.

If you want to show Yes No, just add 4 as third parameter -

[System.Windows.Forms.MessageBox]::Show("We are proceeding with next step." , "Status" , 4)
Now, this will display a Messagebox like this -

How to get values from Messagebox?
As you know, when you press any button, you need to get the result and work upon the decision -
$OUTPUT= [System.Windows.Forms.MessageBox]::Show("We are proceeding with next step." , "Status" , 4)
if ($OUTPUT -eq "YES" )
{ something

{ something else

The value of button pressed is stored in $OUTPUT variable. This variable can then be used for your programming logic.

I have given just a primer how to use Messagebox class. But, if you want to go indepth of System.Windows.Forms.MessageBox class, you may look for the link below -


  1. Nice Work! Exactly what I was happening to be looking for this afternoon. Great Job!

  2. I like this a lot, thanks for publishing. Can you help me with part of your script. The somethine else. I want it to call a .bat file in a different location. can you show me how to do that please.

  3. Hi John,

    To call a bat file from Powershell, you may use below lines (Assuming that batch file is named 'som.bat'. Below line will be sufficient:

    &cmd /c som.bat

    & is for calling any executable inside Powershell.
    /c is to run the statement and terminate command shell. Eventually it will return to Powershell.

  4. Thank you for publishing this post.
    Alex D.

  5. I just used this to make a messagebox appear when there's an error in a powershell script that emails me the results... Very useful! I don't need to go and check the results, as the messagebox tells me! Very useful :)

  6. I have an issue in which ISE wants to hide the MessageBox. I have tried a variety of different methods, but must be missing something. Here is my code snippet.
    Function Show-MessageBoxA
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
    $responseA=[System.Windows.Forms.MessageBox]::Show("You Have Selected Logfile - $fileName - Is This Correct?", "Status", 4)
    Set-Variable -Name _ResponseA ($responseA) -Scope "Global"


  7. Thank's for useful and well-written blog! If I may add, you can also use the "wshell"
    Sample code based on ( at least for PowerShell Versions >= 3.0 )

    $a = new-object -comobject
    $intAnswer = $a.popup("Hello World!", `

    Best Regards

  8. great tutorial, simple and concise.

  9. Thanks you for this information about message box, glad to visit this post.
    Display Boxes