You can use PowerShell to show pop-up or toast messages to notify Windows users about important events. To get information from the user or confirm an action, you can use PowerShell to display toast (pup-up) notifications or modal dialog boxes.
Display a Pop-Up Message with PowerShell
If you want to display a simple modal dialogue box on a desktop, you can use the Wscript class. To display a pop-up window with your message text and the OK button, use the following PowerShell code:
$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("The report generation script is successfully completed!")
Using the various properties of the Popup method, you can customize the appearance of this modal window and add action buttons to it. For example, to display a pop-up prompt with Yes and No buttons, run:
$Output = $wshell.Popup("The script completed successfully. Do you want to view a report?",0,"The report is ready",4+32)
If the user clicks Yes, the command returns 6, and if No, it returns 7. You can run an action or exit the script, depending on the user’s choice.
switch ($Output) { 7 {$wshell.Popup('Pressed No')} 6 {$wshell.Popup('Pressed Yes')} default {$wshell.Popup('Invalid input')} }
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). Number of seconds before the message window closes automatically; - <
Title
> —string (optional). The title text (caption) of the pop-up window; - <
Type
> —number (optional). A combination of flags that determine the type of buttons and icons.
Possible values for the Type flag:
- 0 — only OK button;
- 1 — OK and Cancel;
- 2 — Stop, Retry, and Skip;
- 3 — Yes, No, and Cancel;
- 4 — Yes and No;
- 5 — Retry and Cancel;
- 16 — Stop icon;
- 32 — Question icon;
- 48 — Exclamation icon;
- 64 — Information icon.
The Popup method returns an integer value that allows you to know which button a user has clicked. 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.
Use the Windows Forms class when you need to prompt the user for information.
Add-Type -AssemblyName System.Windows.Forms
$input = [Microsoft.VisualBasic.Interaction]::InputBox("Enter your username:", " Require user input", "")
To process user input:
if ([string]::IsNullOrWhiteSpace($input)) { Write-Host " No information provided" } else { Write-Host " You have entered $input" }
If you want to display a modal dialogue on top of all windows on the desktop:
($ModalTop = New-Object 'System.Windows.Forms.Form').TopMost = $True
[System.Windows.Forms.MessageBox]::Show($ModalTop,"Your text", " Your Caption", 4, 48)
Send a Toast Notification from PowerShell Script
Use the Windows Forms class to display more attractive toast (balloon) notifications. The following PowerShell code displays a pop-up message next to the Windows notification bar that disappears after 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 a pop-up message text for the Windows user'
$balmsg.BalloonTipTitle = "Attention $Env:USERNAME"
$balmsg.Visible = $true
$balmsg.ShowBalloonTip(20000)
To create PowerShell pop-up notifications in Windows, you can use the third-party BurntToast module from the PowerShell gallery. Install the module:
Install-Module -Name BurntToast
For example, now you can easily add a colorful notification to the script from the post “How to automatically disable Wi-Fi when Ethernet cable is connected”:
New-BurntToastNotification -Text " Wi-Fi network disconnection", " Since your device was connected to a high-speed Ethernet LAN, you have been disconnected from your Wi-Fi network" -AppLogo C:\PS\changenetwork.png
Showing a Pop-Up Message on Remote Computer Using PowerShell
You can use PowerShell to send a toast message to a user on a remote computer. First, list the user sessions on the remote computer (in the case of an RDS server):
qwinsta /server:Mun-RDS1
To send a pop-up message to a specific user session on a remote computer:
MSG jsmith /server:Mun-RDS1 "Please restart the SUPGUI client in 5 minutes!”
If you want to send a popup message to all users, use * instead of the username:
MSG * /server:Mun-RDS1 " The server will be restarted in 10 minutes. Save your files and open documents!"
To send a pop-up graphical notification to a remote computer, you can use the RemoteSendToasNotification.ps1 script from our GitHub repo ( https://github.com/maxbakhub/winposh/blob/main/WindowsDesktopManagement/RemoteSendToasNotification.ps1). The Invoke-Command cmdlet that is used to establish the connection requires that WinRM be enabled and configured on the remote computer.
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