PowershellWindows

How to Show a Pop-Up or Balloon Tip Notification from PowerShell

By September 25, 2019October 30th, 2019No Comments

Although PowerShell is a console language, sometimes it is necessary to notify a user from a PS script about a particular event or prompt them to do something. For example, you can display a pop-up notification or balloon tip about the completion of a heavy PoSh script or when an important event occurs.

The easiest way is to display a window containing any text using the Windows script subsystem (Wscript) call from PowerShell.

This PowerShell code will show a common window with your message and the OK button.

$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("The report generation script is successfully completed!")

Wscript - powershell show a windows with the notification

Using different properties of the Popup method, you can customize the appearance of the modal window containing your message text. You can also return to the script the user’s answer (Yes/No).

display a pop-up message box with PowerShell with yes/no

$Output = $wshell.Popup("The report generation script is successfully completed! Do you want to display a report on the screen?",0,"The report is ready",4+32)

The general syntax and the available parameters of the Popup method:

Popup(<Text>,<SecondsToWait>,<Title>,<Type>)

Parameters:

  • <Text> — a message text (string);
  • <SecondsToWait> — a number (optional). The number of seconds after which the message window will be automatically closed;
  • <Title> —string (optional). The title text (caption) of the pop-up window;
  • <Type> —number (optional). The combination of flags that determines the type of buttons and icons.

Possible Type flag values:

  • 0 — OK button;
  • 1 — OK and Cancel buttons;
  • 2 — Stop, Retry and Skip buttons;
  • 3 — Yes, No and Cancel buttons;
  • 4 — Yes and No buttons;
  • 5 — Retry and Cancel buttons;
  • 16 — Stop icon;
  • 32 — Question icon;
  • 48 — Exclamation icon;
  • 64 — Information icon.

The Popup method returns an integer that allows to know, which button was clicked by a user. Possible values:

  • -1 — timeout;
  • 1 — OK button;
  • 2 — Cancel button;
  • 3 — Stop button;
  • 4 — Retry button;
  • 5 — Skip button;
  • 6 — Yes button;
  • 7 — No button.

More attractive pop-up messages (balloon tips) may be displayed in Windows 7, 8.1 & 10 through the Windows Forms API. The following PowerShell code will show a pop-up message next to the Windows 10 Notification bar that will automatically disappear in 20 seconds.

Add-Type -AssemblyName System.Windows.Forms
$global:balmsg = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balmsg.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balmsg.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balmsg.BalloonTipText = ‘This is the pop-up message text for the Windows 10 user'
$balmsg.BalloonTipTitle = "Attention $Env:USERNAME"
$balmsg.Visible = $true
$balmsg.ShowBalloonTip(20000)

balloon tip notification with PowerShell in windows 10

To create colorful pop-up messages in Windows 10 (PowerShell 5.0+), you can also use a separate PowerShell module BurntToast from the PowerShell gallery.

The module is installed from the online repository using the Windows 10 Package Manager:
Install-Module -Name BurntToast

For example, now you can easily add a colorful notification to the script:

New-BurntToastNotification -Text "Disconnecting from Wi-Fi network", "You have been disconnected from your Wi-Fi network since your device was connected to a high-speed Ethernet LAN" -AppLogo C:\PS\changenetwork.png

Therefore, you know how to display a notification to a user with PowerShell. If a user has speakers, you can even play the favorite melody:

[console]::beep(440,500)
[console]::beep(440,500)
[console]::beep(440,500)
[console]::beep(349,350)
[console]::beep(523,150)
[console]::beep(440,500)
[console]::beep(349,350)
[console]::beep(523,150)
[console]::beep(440,1000)
[console]::beep(659,500)
[console]::beep(659,500)
[console]::beep(659,500)
[console]::beep(698,350)
[console]::beep(523,150)
[console]::beep(415,500)
[console]::beep(349,350)
[console]::beep(523,150)
[console]::beep(440,1000)
[console]::beep(880,500)
[console]::beep(440,350)
[console]::beep(440,150)
[console]::beep(880,500)
[console]::beep(830,250)
[console]::beep(784,250)
[console]::beep(740,125)
[console]::beep(698,125)
[console]::beep(740,250)
[console]::beep(455,250)
[console]::beep(622,500)
[console]::beep(587,250)
[console]::beep(554,250)
[console]::beep(523,125)
[console]::beep(466,125)
[console]::beep(523,250)
[console]::beep(349,125)
[console]::beep(415,500)
[console]::beep(349,375)
[console]::beep(440,125)
[console]::beep(523,500)
[console]::beep(440,375)
[console]::beep(523,125)
[console]::beep(659,1000)
[console]::beep(880,500)
[console]::beep(440,350)
[console]::beep(440,150)
[console]::beep(880,500)
[console]::beep(830,250)
[console]::beep(784,250)
[console]::beep(740,125)
[console]::beep(698,125)
[console]::beep(740,250)
[console]::beep(455,250)
[console]::beep(622,500)
[console]::beep(587,250)
[console]::beep(554,250)
[console]::beep(523,125)
[console]::beep(466,125)
[console]::beep(523,250)
[console]::beep(349,250)
[console]::beep(415,500)
[console]::beep(349,375)
[console]::beep(523,125)
[console]::beep(440,500)
[console]::beep(349,375)
[console]::beep(261,125)
[console]::beep(440,1000)

Leave a Reply