Show a Message Notification from PowerShell

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")

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 Popup method

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.

Using a special module, BurntToast

If you are not restricted to use external modules, you can go with BurntToast.

Install it as follows:

  1. Open PowerShell as Administrator.
  2. Type: Install-Module -Name BurntToast
  3. The module is ready to use.

To display a notification, run the command:

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,

  1. The PowerShell execution policy should be configured to allow third-party modules to be loaded.
  2. If Focus Assist is enabled in Windows 10, it may hide your BurntToast notifications.
  3. The module can be removed with the command Uninstall-Module BurntToast.

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:

If you like this article, please share it using the buttons below. It won't take a lot from you, but it will help us grow. Thanks for your support!

Author: Sergey Tkachenko

Sergey Tkachenko is a software developer who started Winaero back in 2011. On this blog, Sergey is writing about everything connected to Microsoft, Windows and popular software. Follow him on Telegram, Twitter, and YouTube.

2 thoughts on “Show a Message Notification from PowerShell”

Leave a Reply

Your email address will not be published.

Exit mobile version
Using Telegram? Subscribe to the blog channel!
Hello. Add your message here.