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.
Advertisеment
Using message boxes in scripts improves the user experience. For example, if a script performs an action that requires user confirmation (such as “The operation completed successfully” or “An error occurred”), a message box provides a clear and attention-grabbing way to notify the user. Another case is asking for confirmation.
You can ask the user to confirm before performing an important operation, such as deleting files, restarting the computer, etc. Finally, it is a convenient way to notify the user about an error that occurred while the script was running. A message box can provide the user with immediate feedback about what went wrong and how to fix it.
Also, long-running scripts can use a message box to notify the user when they are finished and to attract attention.
Here are the methods I usually use.
Show a Message Box 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")
The 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 Message
The syntax is as follows.
Popup(<Text>,<SecondsToWait>,<Title>,<Type>)
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.
Value | Button |
---|---|
0 | OK |
1 | OK, Cancel |
2 | Abort, Ignore, Retry |
3 | Yes, No, Cancel |
4 | Yes, No |
5 | Retry, Cancel |
16 | Critical |
32 | Question |
48 | Exclamation |
64 | Information |
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 |
---|---|
1 | OK |
2 | Cancel |
3 | Abort |
4 | Retry |
5 | Ignore |
6 | Yes |
7 | No |
-1 | None, message 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')
The result:
Again, you can customize it. For reference, see the following page: MessageBox.Show Method.
Finally, there is a special module for PowerShell you can use when it is possible.
How to Show a Desktop Notification from PowerShell
If you are not restricted to use external modules, you can go with BurntToast.
Install it as follows:
- Open PowerShell as Administrator.
- Type:
Install-Module -Name BurntToast
- The module is ready to use.
- To display a notification, run the commands:
Import-Module BurntToast
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.
Finally, you can remove the module with the command Uninstall-Module BurntToast
if you find no use for it.
That's it!
Support us
Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:
Thanks for sharing. Exactly what I was looking for!
heh, you are welcome!