The PowerShell code discussed in this article will help you understand how to display a pop-up message box using PowerShell.

Venkat Sri

Venkat Sri is a senior Windows system engineer who specialises in PowerShell, Active Directory and virtualization. On 4sysops, he shares useful PowerShell scripts for system administrators.

Not everyone likes command line–based arguments. Some people want to be provided with a message box pop-up for YES or NO options or information/error messages. In this article, we will discuss how to show pop-up messages using PowerShell to serve such purposes. We will focus our discussion on how to present YES/NO, YES/NO/CANCEL, and OK/CANCEL kinds of pop-up messages.

Pop-up messages can be generated in PowerShell using Windows Forms. Although I don’t see any problems with them, they look a bit old and legacy-like. The Windows Presentation Framework provides similar functionality with rich-looking UIs. In this article, we will be using some of the classes from the Windows Presentation Framework assembly to create pop-up messages.

First, you need to import the Presentation Framework–related libraries into the current PowerShell session. You can do this using the following Add-Type statement. Without doing this, we cannot call the MessageBox-related classes that help in pop-up generation.

This helps us access the classes in the Windows Presentation Framework. Now we need to decide what kind of message box we want to show (for example, a simple message box with an OK button, or a message box with “Yes” and “No” prompts). For demonstration purposes, we’ll assume you want to show a pop-up message box with YES and NO buttons. So we’ll put that in a variable using the following command:

Now decide on the title for the pop-up message and the message you want to display:

Also decide what kind of message you are displaying. Is it an error, a warning, or an informational message? Let’s assume it is a warning message:

Now, let’s generate a pop-up message box with the data above. This uses the Show method of the System.Windows.MessageBox class.

The command above will show a pop-up message similar to this:

Yes-No prompt

Yes-No prompt

Your choice (Yes or No) is returned to PowerShell based on the button that you click. You can read this returned value and proceed with script execution accordingly.

Similarly, we can generate a message box with YES, NO, and CANCEL buttons and as an error message using the following code:

This code will generate a pop-up message box similar to this one:

Yes-No-Cancel message box

Yes-No-Cancel message box

To see the list of button types you can have for a pop-up message box, see this TechNet page.

A list of images (icons) you can use for a pop-up message can be found on this page.

And here is a list of options that you can pass to the Show() method of the System.Windows.MessageBox class.

Win the monthly 4sysops member prize for IT pros

Share
1+

Related Posts

3 Comments
  1. Eddie 1 year ago

    This is good, but I need the Yes, No, Cancel button to return an integer so I can use it with switch($result)

    0

    • Thomas 3 months ago

      You can use 'Yes', 'No', 'Cancel' in the switch statement.

      0

  2. james day 3 months ago

    Is there a way to have this message box pop up remotely for a user and the results (yes/no) do something on the admin machine where the script was run?  What I am trying to do is write a script that will prompt users if it's ok for a technician to remote in, and if they click "yes" start SCCM cmrcviewer.exe on the technicians machine.

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

CONTACT US

Please ask IT administration questions in the forum. Any other messages are welcome.

Sending
© 4sysops 2006 - 2017

Log in with your credentials

or    

Forgot your details?

Create Account