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

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.

Add-Type -AssemblyName PresentationCore,PresentationFramework

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:

$ButtonType = [System.Windows.MessageBoxButton]::YesNo

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

$MessageboxTitle = “Test pop-up message title”
$Messageboxbody = “Are you sure you want to stop this script execution?”

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:

$MessageIcon = [System.Windows.MessageBoxImage]::Warning

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

[System.Windows.MessageBox]::Show($Messageboxbody,$MessageboxTitle,$ButtonType,$messageicon)

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:

Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::YesNoCancel
$MessageIcon = [System.Windows.MessageBoxImage]::Error
$MessageBody = "Are you sure you want to delete the log file?"
$MessageTitle = "Confirm Deletion"

$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)

Write-Host "Your choice is $Result"

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.

12 Comments
  1. Eddie 7 years ago

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

    • Thomas 6 years ago

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

  2. james day 6 years 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.

  3. Matt Becker 5 years ago

    This is awesome!!!

  4. Ethel Aardvaark 5 years ago

    For complicated reasons, I’d like to run my script via a scheduled task, but no UI appears (although it seems to be waiting for my input).

    Any ideas on how to make this happen?

    • Bren0 5 years ago

      Ethel, ensure the “Run only when user is logged on” radio button is selected when creating the task in Task Scheduler. Choosing the alternative (i.e. “Run whether user is logged on or not”) will hide the script, including any message boxes you have programmed in.

      avatar
  5. Donald 5 years ago

    This is a good overview of a basic message box. If you’re after more options, check out the AnyBox:

    https://github.com/dm3ll3n/AnyBox

  6. Sriramchandra 4 years ago

    Hi All,

    Currently I'm working on Automation using powershell. Need some Help!!

    Is there a way to have this message box pop up once a website is opened.

    For example.. I Ran a script to open a website.

    start-process <https://xyz.com&gt;.

    So once the site is opened . A pop-up window should come asking if we can continue further.

    Click Yes: Only if i see Login page of the website.
    Click  No:  Start the script again from starting.

    Can you please help with code..

     

    Thanks

    Sriramchandra

    • Leos Marek (Rank 4) 4 years ago

      Hello Sriramchandra,

      Im afraid you cant do this. The command Start-process will launch a browser with provided URL, but thats it. The browser will not send any information back to the script if it loaded the page or not.

      If you want to test a webpage availability, you should use Invoke-RestMethod instead.

      Leos 

  7. Lee 3 years ago

    Great post – I love the way you've broken down the steps and made it easy to understand.

  8. Jarle 2 years ago

    Is it possible for this popup box to close down after a period of time if the user is not responding to it? So the script can continue after lets say 30 sec. if the user doesn't make a choice.

  9. Nicholas Galloway 2 years ago

    Jarle
    #[code]
    $Result = (new-object -comobject wscript.shell).popup(“WARNING”,0,”Box title”,0)
    echo $Result
    #[notes]
    <– 0 after "Warning" is wait time (0=wait for user input / 1=wait 1sec / 2=wait 2sec / etc…)
    <– $Result: (-1)TimedOut (1)ok (2)cancel (3)abort (4)retry (5)ignore (6)yes (7)no (8)??? (10)try-again (11)continue
    <– 0 after "Box Title" sets type of box and user responces. As follows:
    ( ) 0=ok 1=ok/cancel 2=abort/retry/ignore 3=Y/N/Cancel 4=Y/N 5=Retry/Cancel 6=Cancel/try again/continue
    (X) 16=ok 17=ok/cancel 18=abort/retry/ignore 19=Y/N/Cancel 20=Y/N 21=Retry/Cancel 22=Cancel/try again/continue
    (?) 32=ok 33=ok/cancel 33=abort/retry/ignore 34=Y/N/Cancel 35=Y/N 36=Retry/Cancel 37=Cancel/try again/continue
    (!) 48=ok 49=ok/cancel 50=abort/retry/ignore 51=Y/N/Cancel 52=Y/N 53=Retry/Cancel 54=Cancel/try again/continue
    (/!) 64=ok 65=ok/cancel 66=abort/retry/ignore 67=Y/N/Cancel 68=Y/N 53=Retry/Cancel 69=Cancel/try again/continue

Leave a reply to Nicholas Galloway Click here to cancel the reply

Please enclose code in pre tags

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

*

© 4sysops 2006 - 2023

CONTACT US

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

Sending

Log in with your credentials

or    

Forgot your details?

Create Account