If you’d like to show a default MessageBox with some buttons for the user to click, try this function:

function Show-MessageBox
{
  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory=$true,ValueFromPipeline=$false)]
    [String]
    $Text,
    
    [Parameter(Mandatory=$true,ValueFromPipeline=$false)]
    [String]
    $Caption,
    
    [Parameter(Mandatory=$true,ValueFromPipeline=$false)]
    [Windows.MessageBoxButton]
    $Button,
    
    [Parameter(Mandatory=$true,ValueFromPipeline=$false)]
    [Windows.MessageBoxImage]
    $Icon
    
  )
  
  process
  {
    try
    {
      [System.Windows.MessageBox]::Show($Text, $Caption, $Button, $Icon)
    }
    catch
    {
      Write-Warning "Error occured: $_"
    }
  }
}

And here is how you’d use it:

 
PS> Show-MessageBox -Text 'Do you want to reboot now?' -Caption Reboot -Button YesNoCancel -Icon Exclamatio 
 

Twitter This Tip! ReTweet this Tip!

Anonymous
  • Hi All,

    This is great to get the box to appear and click the desired button.

    My question is ... Where do we enter the if statement to reboot the computer (if that is what we want to do)?

    I see within the function there is a $button variable and I am guessing this is what the -Button YesNoCancel is assigned to, and selecting a button it is written to the host ... I am guessing this is the output.

    Therefore I am wondering do we want to then pipe the output to an if statement EG IF Output = yes then Shutdown -f -r -t 0 Else exit or is it there a better way of doing it?

    Show-MessageBox -Text 'Do you want to restart now?' -Caption "DONMAN Setup - Restart Required!" -Button YesNoCancel -Icon Exclamation

  • One could also just do this..

    'community.idera.com/powershell/powertips/b/tips/posts/displaying-message-boxes'

    Or just use this module

    Designed to facilitate script input/output with an easily customizable WPF window.

    'powershellgallery.com/packages/AnyBox/0.3.0'

    Authors site - instructions -

    'donaldmellenbruch.com/post/introducing-the-anybox'

  • All I get is this:
    Unable to find type [Windows.MessageBoxButton].
    At line:15 char:4
    + [Windows.MessageBoxButton]
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (Windows.MessageBoxButton:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

  • Using Windows 7 I cannot see all the icons.  Will they appear in Windows 10?  Is there are way to path to my own icons?