4

PSTip: Use Windows Forms to generate GUI messagebox

While it is great that PowerShell can do so many things in the background without having to interact with it, occasionally it might be useful to have a graphical notification. For example, when a job finishes or when input is required in order for the script to be able to continue. For this purpose a popup window can be displayed; this is possible using the Windows.Forms namespace and in particular the MessageBox class.

The following example will display a MessageBox form with an OK button and the ‘Hello World’ text:

Add-Type -AssemblyName System.Windows.Forms | Out-Null
[System.Windows.Forms.MessageBox]::Show("Hello World")

The MessageBox class can accept more arguments than just a single string. To explore the possible constructors the Show method can be called without any parameters displaying all OverloadDefinitions. In the following code the OverloadDefinitions and the total count of possible definitions will be displayed:

[System.Windows.Forms.MessageBox]::Show
[System.Windows.Forms.MessageBox]::Show.OverloadDefinitions.Count

The output from the previous command shows that it is possible to change the MessageBoxButtons and that it is possible to add a MessageBoxIcon. The GetNames method of the Enum class can be called to enumerate the possible entries for these options:

[Enum]::GetNames([System.Windows.Forms.MessageBoxButtons])
[Enum]::GetNames([System.Windows.Forms.MessageBoxIcon])

The parameters for Title, Caption, MessageBoxButtons and MessageBoxIcon will be specified to create a new MessageBox. The output will be stored in the $MessageBox variable and can be used for further execution in the script:

$MessageBox = [System.Windows.Forms.MessageBox]::Show(
    'Text','Caption','YesNoCancel','Question'
)

For more information about the MessageBox class and to view all the definitions have a look at its MSDN entry:

MessageBox Class

Filed in: Tips and Tricks

4 Responses to "PSTip: Use Windows Forms to generate GUI messagebox"

  1. Serguei Kouzmine says:

    Please view my article on using all sorts of Windows Forms / WPF for Powershell adding functionality
    http://www.codeproject.com/Articles/799161/Dealing-with-Powershell-Inputs-via-Baasic-Windows-F

Leave a Reply

Submit Comment

© 2017 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: