≡ Menu

How to get Pop up message box using Powershell

AFAIK, there are two ways available to generate a Pop-up message box using powershell.

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 http://techibee.com to stay tuned”, “PowerShellScripts.blogspot.com”, [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information)

{ 12 comments… add one }

  • 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)
      }

  • 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.

Leave a Comment