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!")
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).
$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)
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 command:
Install-Module -Name BurntToast
For example, now you can easily add a colorful notification to the script from the article “How to automatically disable Wi-Fi when Ethernet cable is connected”:
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)
2 comments
The ballon tooltip seems very nice. But it adds an icon to the toolbar and you have to run a code to kill it:
$script:balmsg.Dispose()
The problem is that you don’t know if the user saw the notification… So you don’t have a way to know when to kill that icon.
And if you don’t kill it, the user must close PowerShell to get rid of that icon…
Thank you! Really useful