≡ Menu

How to get Pop up message box using Powershell

There are two good and easy ways available to generate a Pop-up message box using PowerShell. These methods uses DotNet and Windows Shell so works in any version of Operating System.

Simple way:-

In this powershell relies on a method which is available in windows shell object(WSH). In this you can control four factors, “Message box title”, “Message”, “timeout for message box”, “box type”.

Below is a simple example.

$a = new-object -comobject wscript.shell

$b = $a.popup(“This is a test message from http://Techibee.com “,0,“Test message from techibee”,1)

Second and effective method:-

In this we can make use of dotnet windows forms to generate the pop-up message boxes. Though code looks bit heavy it works very well and has lot of flexibility.

[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

[Windows.Forms.MessageBox]::Show(“Test message from Techibee.com!. Subscribe to news letters, RSS feeds at https://techibee.com to stay tuned”, “PowerShellScripts.blogspot.com”, [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information)

Comments on this entry are closed.

  • newuser October 19, 2010, 8:09 pm

    Another way….
    (source 1= http://powershell.com/cs/media/p/385.aspx“)
    [void][System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’)

    $result = [Microsoft.VisualBasic.Interaction]::MsgBox(“Do you agree?”, ‘YesNoCancel,Question’, “Respond please”)

    switch ($result) {
    ‘Yes’ { “Ah good” }
    ‘No’ { “Sorry to hear that” }
    ‘Cancel’ { “Bye…” }
    }

  • Ivan November 25, 2010, 6:05 pm

    Is there a way to send this message to all users or computers in a specific OU in Active Directory?
    I want to use as anetsend replacement.
    Thanks

  • Sitaram Pamarthi December 1, 2010, 8:09 pm

    You can try executing the code snippets in article using invoke-command cmdlet.

    $comps = Get-QADComputer -SearchRoot “cn=computers,dc=domain,dc=com” | select Name
    foreach ($comp in $comps) {
    $pc=$comp.name
    invoke-command -computername $pc {
    <
    }

  • Doug June 8, 2012, 3:56 am

    Little cleaner version..

    $result = [System.Windows.Forms.MessageBox]::Show(“Continue Task?”,”What a Mess”, “YesNo” , “Information” , “Button1”)

    $result contains the result from Button “Yes” or “No” whichever is pressed.

  • Josef November 15, 2012, 6:01 pm

    Will this code be possible to execute from a central management server to all citrix servers, telling all users on each server with a popoup that they should logout now ?

  • Rob June 12, 2013, 4:32 pm

    need some help.
    here is a portion of my script.
    what needs to happen is that if i click ok the script will run.
    but if i click cancel the script should stop and close the powershell interface.

    at the moment the pop up runs and if i click cancel the script continues to run.

    $a = new-object -comobject wscript.shell
    $b = $a.popup(“Press OK to start checking the Cluster Services.“,0,”Message of the day”,1)

    $Srvname = “server name goes here”
    $ping = new-object System.Net.NetworkInformation.Ping
    $shell = New-Object -ComObject wscript.shell
    $test = $ping.send($Srvname)
    if (!$?)
    {
    $shell.popup(“Couldn’t find the IP address of the HOST mentioned”)
    }
    elseif ($test.status -eq “Success”)
    {
    $shell.popup(“Cluster Responded to Ping”)
    }
    Else
    {
    $status = $test.status
    $shell.popup(“Failed with status: $status”)
    }
    #end

    • Sitaram Pamarthi July 6, 2013, 6:16 pm

      @Rob, I see that you are not using the return value from pop-up message box in your code. Try something like this.

      $a = new-object -comobject wscript.shell
      $b = $a.popup(“Press OK to start checking the Cluster Services.“,0,”Message of the day”,1)
      if($b -eq 1) {
      //rest of my code here that needs be processed when OK is selected.
      }

      if($b -eq 2) {

      //cancle was selected in the box. So should exit
      exit(1)
      }

      • Rohan Jannu April 5, 2016, 2:39 am

        Can i define timeout for this?? if user didn’t answer the popup window to continue the script further actions??

        • Rohan Jannu April 5, 2016, 6:42 pm

          Send pop-up to remote computer using powershell…………

          Hello,

          Is there any way to use Powershell to send a pop-up to remote computer?

          I found following script:

          $a = new-object -comobject wscript.shell
          $b = $a.popup(“This is a test message from http://Techibee.com “,0,”Test message from techibee”,1)

          Here: http://techibee.com/powershell/how-to-get-pop-up-message-box-using-powershell/839

          It works locally, but it does not seem to be working on remote computer (through invoke-command…

          $colComputers = get-content C:\admin\Network_Message_pcs.txt
          foreach ($strComputer in $colComputers)
          {

          invoke-command -computername $strComputer {

          $e = new-object -comobject wscript.shell
          $f = $e.popup(“This is a test message from http://Techibee.com “,0,”Test message from techibee”,1)

          }

          }

          If you have any ideas on how to get pop-up on remote computer, help would be greatly appreciated.

          Thank you,

          • KL May 25, 2016, 2:20 am

            The invoke command will likely not work on remote desktops by default because of remote execution policy. To get around it, write your powershell script and save it to an accessible network location. Then create a batch or command file that calls the powershell script, for example:
            Powershell -noprofile -executionpolicy bypass -file PoweshellScript.ps1

            As for your previous question about timeout, that’s what the “0” in $b = $a.popup(“This is a test message from http://Techibee.com “,0,”Test message from techibee”,1) is for. Change the value to define a timeout in seconds. I don’t know if the timeout registers as an OK or a cancel though.

  • web page August 17, 2013, 7:40 am

    Hi! This is my first comment here so I just wanted to give a quick
    shout out and tell you I genuinely enjoy
    reading your posts. Can you recommend any other blogs/websites/forums that go
    over the same topics? Thanks a lot!

  • Simpsons Tapped Out Hack September 1, 2013, 6:52 am

    Spot on with this write-up, I actually believe that this amazing site needs a great
    deal more attention. I’ll probably be back again to read through more, thanks for the info!

  • Joseph April 4, 2014, 1:09 am

    Hi,

    I’m really new to powershell and just learned how to make pop up appears.

    I was wondering if someone could help me out by showing me how to make it appear when a user performs a specific action? Or is this outside the range of Powershell?

    Thanks in advance.

    • Sitaram Pamarthi April 4, 2014, 8:27 pm

      Joseph, on what kind of action, you want to give a pop-up. Giving exact scenario gives us enough flexibility help.

  • gain weight January 17, 2015, 4:34 am

    Good write-up. I definitely appreciate this website. Continue the good work!

  • Stephen McFall February 5, 2015, 5:43 pm

    Hi Guys looking to modify this script
    $a = new-object -comobject wscript.shell
    $b = $a.popup(“Press OK to start checking the Cluster Services.“,0,”Message of the day”,1)
    if($b -eq 1) {
    //rest of my code here that needs be processed when OK is selected.
    }

    if($b -eq 2) {

    //cancle was selected in the box. So should exit
    exit(1)

    To change the message box appearance to show the information symbol.

    [Windows.Forms.MessageBoxIcon]::Information) seems to be the input?

    • Ethan B. October 19, 2016, 12:27 am

      Change the 0 at $b line to a 64. If it isn’t 64, then 16 should do it.

  • Chad March 23, 2015, 11:38 pm

    This seems to work for me

    If (Test-Path $inputFile){
    Else {
    [System.Windows.Forms.MessageBox]::Show(“THE REQUIRED $inputFile FILE WAS NOT FOUND. PLEASE ENSURE $inputFile IS IN THE SAME DIRECTORY AS THIS SCRIPT”,”!!!!FILE NOT FOUND!!!!”,”OK”,”Warning”)
    exit(1)
    }