Beta 42

Research and Development

Menu

PowerShell - Showing MsgBox

Ever wanted to display a dialog box from PowerShell rather than spitting out console text?

Try the following 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
}