PowerShell is an advanced form of command prompt. It is extended with a huge set of ready-to-use cmdlets and comes with the ability to use .NET framework/C# in various scenarios. Windows includes a GUI tool, PowerShell ISE, which allows editing and debugging scripts in a useful way. Sometimes you many need to show a notification from a PowerShell script to inform the user that some task is done. Here are some methods you can use.
A message from your PowerShell script may inform the user that your long-running script has finished its work. Or, it may tell that something is wrong or something important has happened. Here are the methods I usually use.
Show a Message Notification from PowerShell
The simplest method involves the classic Windows Scripting Host app.
With PowerShell, it is easy to create an instance of a COM object. In our case, we need the Windows.Shell object from WSH. It can be created with the following command:
$wsh = New-Object -ComObject Wscript.Shell
Now, we can use our
$wsh object to call methods available for
Wscript.Shell. One of them is
Popup, this is what we need. The following code can be used:
$wsh = New-Object -ComObject Wscript.Shell $wsh.Popup("Hello from Winaero")
Popup method can be customized. For example, you can specify its title, assign one of the default dialog icons, or show extra buttons.
Customize the Popup method
The syntax is as follows.
Text is the text you want to show in the message.
SecondsToWait is an integer containing the number of seconds that the box will display for until dismissed. If zero or omitted, the message box stays until the user dismisses.
Title is a string containing the title that will appear as the title of the message.
Type is an integer that corresponds to a particular look and behavior defined in the following table.
To get the desired icon and buttons, combine the values. E.g., to compose a question, use 1+32 as your
Type value. See the following example:
The Popup method can also return the button which the user clicked to dismiss the pop-up message box. Refer to the following table:
|Return value||Button clicked|
box was dismissed automatically (timeout)
You can handle the return value as follows:
$result = $wsh.Popup("Do you like Winaero?",0,"A question from PS",1+32)
Alternatively, you can use a .NET Framework call to display a message.
Using MessageBox from .NET Framework
The command you need to use looks as follows:
[System.Windows.MessageBox]::Show('Hello from Winaero')
Again, you can customize it. For reference, see the following page:
Finally, there is a special module for PowerShell you can use when it is possible.
Using a special module, BurntToast
If you are not restricted to use external modules, you can go with BurntToast.
Install it as follows:
- Open PowerShell as Administrator.
Install-Module -Name BurntToast
- The module is ready to use.
To display a notification, run the command:
New-BurntToastNotification -Text "Winaero","Hello from PowerShell"
See the PowerShell Gallery for the complete details and instructions.
Please keep in mind the following. To see your notification,
- The PowerShell execution policy should be configured to allow third-party modules to be loaded.
- If Focus Assist is enabled in Windows 10, it may hide your BurntToast notifications.
- The module can be removed with the command