Powershell

PowerShell Popup Message Box

Most of the PowerShell operations are command-line based, but you can also get graphical output. PowerShell uses the .NET framework, so it can produce graphical outputs just like C# and VB.NET. PowerShell does allow you to display a graphical popup message box in the output. A popup message box may contain any message according to the requirement of the user.

This guide provides a demonstration to create a popup message box using PowerShell. Moreover, you would also learn to create a popup message box with extended functionalities.

How to configure PowerShell for a popup message box

PowerShell’s graphical interfaces can be created by either Windows Forms or Windows Presentation Framework. To create a popup message box in PowerShell, the [Systems.Windows.MessageBox] method of Windows Presentation Framework is used that is not imported in PowerShell by default. To import the Windows Presentation Framework assembly in the current PowerShell’s session, you must make use of the following command:

> Add-Type -AssemblyNamePresentationFramework

Note: If you try to create a popup message box without importing the assembly of Presentation Framework, you will encounter the following error:

> [System.Windows.MessageBox]::Show("linuxhint")

How to make a popup message box in PowerShell

Once you have added the assembly of Windows Presentation Framework, you can now use the [System.Windows.MessageBox] method to create a popup message box in PowerShell.

Creating a simple PowerShell popup message box

Let’s try a simple popup message box by executing the Show method of MessageBox class in PowerShell. In the following command, a string is passed to the Show method and you would notice a message box is displayed that contains that string as a message with an OK button.

Note: We have passed a single string to the Show method, and the title, button type, and button icon of the popup message box are obtained by default.

> [System.Windows.MessageBox]::Show("Welcome to linuxhint!")

Creating a customized PowerShell popup message box

A PowerShell message box comprises of the following components:

MessageBox Title: Appears on the header of the message box:

MessageBox Body: Refers to the body of the message box.

ButtonType: This refers to the category of the button and it could be OK, OKCancel, YesNo, and YesNoCancel. If none of the mentioned parameters are passed, PowerShell prints OK as a default button type.

MessageIcon: This parameter prints an icon of the message box. It accepts values such as Warning. Error, Stop, Hand, None and so on. Each value is represented by associated symbols in the output. The default value of MessageIcon is None (if value is not provided).

All these instances of a Message Box can be customized by using the options supported by each instance of popup MessageBox.

The Show method is considered the main stakeholder in customizing the popup message box and it accepts parameters such as title, body, and button properties that can be passed as a parameter to the Show method. The syntax to use Show method in such scenario is provided below:

[System.Windows.MessageBox]::Show("<Body>", "<Title>", "<Button-Type>", "<Icon>")

The Show method of the MessageBox class provides some extended functionality to customize the popup message box. By utilizing the properties of the Show method, we have executed the following command to create a PowerShell popup message box:

> [System.Windows.MessageBox]::Show("Welcome to linuxhint!", "Welcome Message", "OK", "None")

Another command is utilized here to create a PowerShell popup message box that contains the following properties:

Body: “Are you a Windows user?”
Title: Question
Button: YesNo
Icon: Question

> [System.Windows.MessageBox]::Show("Are you a Windows user?", "Question", "YesNo", "Question")

Conclusion

PowerShell can provide a graphical interface of several operations that are being performed in the console. The .NET framework is the key facilitator in this regard. In this PowerShell post, you have learned to create a pop-up messagebox with various functionalities. You have learned to create a simple message box with default properties. Moreover, this guide also demonstrates the creation of a pop-up message box with custom properties. These pop-up messages can be used to show information, a warning, a question, and much more.

About the author

Adnan Shabbir