How-to: Display user messages in a popup messagebox.

A function to display messages in a popup message box, with options for OK/Cancel, Yes/No or Retry/Cancel buttons.

function msgbox {
param (
    [string]$Title = 'Message box title',   
    [string]$buttons = 'OKCancel'
# This function displays a message box by calling the .Net Windows.Forms (MessageBox class)
# Load the assembly
Add-Type -AssemblyName System.Windows.Forms | Out-Null
# Define the button types
switch ($buttons) {
   'ok' {$btn = [System.Windows.Forms.MessageBoxButtons]::OK; break}
   'okcancel' {$btn = [System.Windows.Forms.MessageBoxButtons]::OKCancel; break}
   'YesNoCancel' {$btn = [System.Windows.Forms.MessageBoxButtons]::YesNoCancel; break}
   'YesNo' {$btn = [System.Windows.Forms.MessageBoxButtons]::yesno; break}
   'RetryCancel'{$btn = [System.Windows.Forms.MessageBoxButtons]::RetryCancel; break}
   default {$btn = [System.Windows.Forms.MessageBoxButtons]::RetryCancel; break}
# Display the message box

# Display the option chosen by the user:

The MessageBox Button options are from the standard .Net framework MessageBox class.


PS C:> msgbox "test message"

PS C:> $result = msgbox -message "test message" -title "Hello" -buttons YesNoCancel
PS C:> $result = msgbox "test message" "Hello" yesnocancel

“My candle-burning vampire child who stays up all night long and gets phone messages from Marilyn Manson. Every mother’s dream” ~ Fiona Apple’s mother

Related PowerShell Cmdlets:

Functions and Filters - Write a named block of code.

Copyright © 1999-2020
Some rights reserved