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:
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:
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: