Show a message box with PowerShell

A simple function to show a .NET message box:

function ShowMessageBox
{
    # $MessageType: 'Error', 'Information', 'Warning'

    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [String]$MainMessage,
        [Parameter(Mandatory = $true, Position = 1)]
        [String]$MessageHeader,
        [Parameter(Mandatory = $true, Position = 2)]
        [String]$ButtonsToShow,
        [Parameter(Mandatory = $true, Position = 3)]
        [String]$MessageType
    )

    $msgBoxInput = [System.Windows.MessageBox]::Show($MainMessage, $MessageHeader, $ButtonsToShow, $MessageType)

    Switch ($msgBoxInput)
    {
        'Yes'
        {
            # do something ...
            return "Yes"
        }

        'No'
        {
            # do something ...
            return "No"
        }

        'Cancel'
        {
            # do something ...
            return "Cancel"
        }
    }

}



$myAnswer = ShowMessageBox "Are you the oldest person in your family?" "Warning" "YesNo" "Warning"


$myAnswer
PowerShell

The result:

And if you press the “Yes” button:

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *