Home HowTo Dialogs Displaying Message Box
 

Displaying Message Box

Here’s some very simple and straight-forward code to display a custom MsgBox in PowerShell.

You can specify message text, header, default buttons, and icon to display. Your code can then respond to the result with a condition.

Displaying a MsgBox dialog

The Add-Type statement loads the .NET assemblies required to display the message box. This statement is necessary only in a console window because a console PowerShell does not load graphical assemblies. Inside the ISE editor, you could get away with skipping Add-Type, just because the type System.Windows.Forms is loaded by the ISE editor by default.

  

# add the required .NET assembly:
Add-Type -AssemblyName System.Windows.Forms

# show the MsgBox:
$result = [System.Windows.Forms.MessageBox]::Show('Do you want to restart?', 'Warning', 'YesNo', 'Warning')

# check the result:
if ($result -eq 'Yes')
{
  Restart-Computer -WhatIf
}
else
{
  Write-Warning 'Skipping Restart'
}

  Displaying a MsgBox and looking at the results

When you call Show() with an invalid parameter (such as “Maybe” instead of “YesNo”), the red error message will list the legal values you can use.