Showing MsgBox

by Mar 16, 2012

Ever wanted to display a dialog box from PowerShell rather than spitting out console text? Then try this function:

function Show-MsgBox {
  param
  (
    [Parameter(Mandatory=$true)]
    [String]$Text,
    
    [String]$Title = 'Message',
    
    [String]
    $Icon = 'YesNo,Information'
  )

  Add-Type -AssemblyName 'Microsoft.VisualBasic'
  
  [Microsoft.VisualBasic.Interaction]::MsgBox($text, $icon, $title)
}

It is really easy to use: simply tell it what to display:

Show-MsgBox -Text 'Reboot system?' -Title 'Warning' -Icon 'YesNoCancel,Question'

And this makes it a useful function:

$result = Show-MsgBox -Text 'Reboot system?' -Title 'Warning' -Icon 'YesNoCancel,Question'
if ($result -eq 'Yes') {
  Restart-Computer -Force
}

Twitter This Tip! ReTweet this Tip!