Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun Get MessageBox

Posted on March 4, 2011March 28, 2011

Today's Friday Fun offers a way for you to graphically interact with your PowerShell scripts and functions without resorting to a lot of complex Winform scripting. I have a function that you can use to display an interactive message box complete with buttons like Yes, No or Cancel. You can either use the message box to display a message to the user or return input.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

The function is a wrapper for the MsgBox() method of the [Microsoft.VisualBasic.Interaction] .NET class.

[cc lang="PowerShell"]
Function Get-MessageBox {
Param (
[Parameter(Position=0,Mandatory=$True,HelpMessage="Specify a display message")]
[ValidateNotNullorEmpty()]
[ValidateScript({$_.Length -le 1024})]
[string]$Message,

[ValidateSet("OkOnly","OkCancel","AbortRetryIgnore","YesNoCancel","YesNo","RetryCancel")]
[string]$Button="OkOnly",

[ValidateSet("Critical", "Question", "Exclamation", "Information")]
[string]$Icon="Information",

[string]$Title,
[switch]$Passthru
)

#load the required .NET assembly
Add-Type -AssemblyName "Microsoft.VisualBasic"
#Invoke the message box and save the value from any button clicks to a variable
$returnValue=[Microsoft.Visualbasic.Interaction]::Msgbox($message,"$button,$icon",$title)

#write return value if -Passthru is called
if ($passthru)
{
Write-Output $returnValue
}

} #end function
[/cc]

The method allows you to specify the message text (up to 1024 characters), an icon, a button set and a title. The only value you really need to specify is a message. The default icon is Information and the default title is blank. The function uses the ValidateSet attribute to verify that the right value is specified for icon and title.

When you run this function, the user has to click on one of the buttons. The return value is stored in a variable.

[cc lang="PowerShell"]
$returnValue=[Microsoft.Visualbasic.Interaction]::Msgbox($message,"$button,$icon",$title)
[/cc]

I wrote the function assuming that most of the time all you want is to present an informational message with an OK button which means you don't really need to see the return value. Thus the default behavior is NOT to write it to the pipeline. However, if you use -Passthru, then the value will be written to the pipeline. This allows you to use code like:

[cc lang="PowerShell"]
$rc=Get-Messagebox -message "Do you know what you're doing?" -icon "exclamation" -button "YesNoCancel" -title "Hey $env:username!!" -passthru

Switch ($rc) {
"Yes" {"I hope your resume is up to date."}
"No" {"Wise move."}
"cancel" {"When in doubt, punt."}
Default {"nothing returned"}
}
[/cc]

Here's the actual message box.

The message parameter value must be a string and you can create multi-line entries using `n.

[cc lang="PowerShell"]
PS C:\> $m="Hello $env:username `n It is now $(Get-Date)"
PS C:\> Get-MessageBox -Message $m -Title "Greeting"
[/cc]

The script file with the function also includes an optional alias, gmd. You can download the script file here. The full script also has comment-based help. Enjoy.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d