PowerShell msgbox

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

function msgbox {
param (
    [string]$Message,
    [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}
   'AbortRetryIgnore' {$btn = [System.Windows.Forms.MessageBoxButtons]::AbortRetryIgnore; 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
  $Return=[System.Windows.Forms.MessageBox]::Show($Message,$Title,$btn)
  $Return
}

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

Examples

PS C:> msgbox "test message"

PS C:> $res = msgbox -message "test message" -title "Something important" -buttons abortretryignore
or
PS C:> $res = msgbox "test message" "Something important" abortretryignore

“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 Commands:

Functions and Filters - Write a named block of code.


Copyright © SS64.com 1999-2019
Some rights reserved