Microsoft enhances PowerShell on Windows 10 with significant differences from the Command Prompt, an outdated command-line tool. To explore these differences, refer to the article comparing Command Prompt and PowerShell here.
With PowerShell, you can easily style balloon notifications using PowerShell, following the guidance below.
The most common notification in PowerShell involves text stored on the hosting server. Employ any Write- * cmdlet command to write text to the console. Error or warning notifications use Write-Error cmdlet or Write-Warning cmdlet, while the standard Write-Host command notifies you when a process or task is completed.
Beyond the console, another approach to capture user attention upon task completion or error occurrence is to display a popup window on the screen. This is achieved using System.Windows.MessageBox and employing the appropriate method to show notifications to the user.
Both notification types are useful and provide feedback to users when needed. However, there's another type of notification known as the Balloon-style notification.
Most users are familiar with Balloon-style notifications, especially for Windows update notifications.
This type of balloon notification is mainly seen on Windows 7 but still functions on Windows 10.
So how do you create a Balloon-style notification? The good news is you don't need to dig deep to access this type of notification interface. No need to create WPF with XAML or delve into Windows Forms to build anything.
Instead, all you need to do is work with System.Windows.Forms.NotifyIcon to create a Balloon-style notification using PowerShell. Note that you'll have to add the System.Windows.Forms assembly to your PowerShell session before using the NotifyIcon class.
As mentioned earlier, the first step you need to take is to load the System.Windows.Forms assembly into your PowerShell session:
Load the System.Windows.Forms assembly into your PowerShell session:
This way, you can create the first NotifyIcon object and inspect its properties and methods.
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
The next step is to create the Balloon-style notification.
Inspect the properties and methods of the balloon object:
There are numerous properties and methods, along with useful events for handling the NotifyIcon in the system tray, including the MouseDoubleClick event.
Before initiating the process of setting properties to create the Balloon-style notification, proceed to create an event handler used to remove the icon from the system tray when the user double-clicks on the icon.
[void](Register-ObjectEvent -InputObject $balloon -EventName MouseDoubleClick -SourceIdentifier IconClicked -Action {
#Perform cleanup actions on balloon tip
Dispose of the NotifyIcon object to clean up the balloon tip.
Unregister the event with the source identifier 'IconClicked'.
Remove the job with the name 'IconClicked'.
Remove the global variable 'balloon' from the scope.
})
From now on, when users double-click the icon, the icon will be removed by disposing of the NotifyIcon object and handling the event.
Now, proceed to build objects by setting up some properties. Start by setting the tray icon for PowerShell ISE by specifying the path through Get-Process
and specifying the Path property.
After obtaining the path, the next step is to extract the icon from the file and apply it to the Icon property.
$path = (Get-Process -id $pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
Using this approach, you can pick any icon from a file and use it as the tray icon for the balloon notification.
If you want to add an icon to the balloon notification similar to what you see in Windows update notifications, you'll need to find a suitable icon by examining the static members of the System.Windows.Forms.ToolTipIcon class.
[System.Windows.Forms.ToolTipIcon] | Get-Member -Static -Type Property
You can use the Warning ToolTipIcon for this type of notification.
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
Next, set a title and message that will be displayed in the balloon notification.
$balloon.BalloonTipText = 'What do you think of this balloon tip?'
$balloon.BalloonTipTitle = 'Attention $Env:USERNAME'
Finally, set the Visible property of the balloon notification to $True:
$balloon.Visible = $true
To display this balloon notification, you will call the ShowBalloonTip method. There are a few parameters you can use.
In this case, specifying an integer notification time, in milliseconds, the notification will display on the system tray. For example, showing the notification for 5 seconds is equivalent to 5000 milliseconds when calling ShowBalloonTip.
$balloon.ShowBalloonTip(5000)
After completing the tasks, simply double-click the PowerShell ISE icon you used to close the icon.
Additionally, you can use a feature called Invoke-BalloonTip (available on GitHub) to create balloon notifications more easily. After downloading, run Invoke-BalloonTip with the following command:
Invoke-BalloonTip -Message 'This is a message from my function' -Title 'Attention!' -MessageType Info
Here, Mytour has guided you on how to create balloon notifications using PowerShell. Hopefully, after this article, you've learned how to style balloon notifications on your Windows computer.