Displaying Message Boxes

by Sep 22, 2016

PowerShell can access all public .NET classes, so it is (fairly) easy to create a message box:

$result = [System.Windows.MessageBox]::Show('Do you want to restart?','Restart','YesNo','Warning') 


$result

However, you would need to know the supported values for the parameters. PowerShell can easily wrap this .NET call into a PowerShell function which then provides IntelliSense for all parameters:

#requires -Version 3.0

Add-Type -AssemblyName PresentationFramework

function Show-MessageBox
{
  param
  (
    [Parameter(Mandatory)]
    [String]
    $Prompt,
    
    [String]$Title = 'PowerShell',
    
    [Windows.MessageBoxButton]$Button = 'OK',
    
    [Windows.MessageBoxImage]$Icon = 'Information'    
  )
  
  [Windows.MessageBox]::Show($Prompt, $Title, $Button, $Icon)
}

When you run this code, you now have a super-easy to use new command called “Show-MessageBox”. It accepts parameters and helps you with auto-completion and IntelliSense to find the right values for you.

Twitter This Tip! ReTweet this Tip!