PowerShell – Windows Form MessageBox helper function

Hi Guys,

Here is a small function I wrote to help me create MessageBoxes for a customer I worked for past year. I needed a way to create easy interactions within my scripts with the user, and switch the actions regarding the user choices.
This function will display the message you want in a Windows Form MessageBox and return to you script the value of the button clicked by the enduser.

Function New-MessageBox {
    <#
        .SYNOPSIS
        Easy the MessageBox generation and display.

        .PARAMETER Message
        Message value is the displayed message inside the MessageBox.

        .PARAMETER Title
        Title value is the MessageBox title.

        .PARAMETER Buttons
        Buttons allows you to choose which buttons are displayed at the bottom of the window.
        Buttons will allow you to switch between actions once the user actions are done.

        .PARAMETER Icon
        Icon allow you to choose the icon you want the MessageBox to display.

        .EXAMPLE
        Show-Message -Message Value -Title Value -Buttons OkCancel -Icon Warning
        Will display a MessageBox with 'Value' as title and as text, with 'Ok' and 'Cancel' buttons and a Warning icon.
        
        .EXAMPLE
        Show-Message -Message Value -Title Value
        Will display a MessageBox with 'Value' as title and as text, with a 'Ok' button.

        .NOTES
        Author: Thomas Prud'homme (AZEO).

        .OUTPUT
        String value of the clicked button.

        .LINK
        https://blog.prudhomme.wtf/powershell-windows-form-messagebox-helper-function
    #>
    Param(
        [Parameter(
            Mandatory   = $true,
            HelpMessage = 'Message value is the displayed message inside the MessageBox.'
        )]
        [String]$Message,

        [Parameter(
            Mandatory   = $true,
            HelpMessage = 'Title value is the MessageBox title.'
        )]
        [String]$Title,

        [ValidateSet('OkCancel','AbortRetryIgnore','YesNoCancel','YesNo','RetryCancel')]
        [String]$Buttons,

        [ValidateSet('Error','Question','Warning','Info')]
        [String]$Icon
    )

    #Load Windows.Forms Assembly
    $null = Add-Type -AssemblyName System.Windows.Forms

    #Affect button value based on $Buttons parameter selected
    switch ($Buttons) {
        'OkCancel' {
            [Int]$Btn = 1
            continue
        }
        'AbortRetryIgnore' {
            [Int]$Btn = 2
            Continue
        }
        'YesNoCancel' {
            [Int]$Btn = 3
            Continue
        }
        'YesNo' {
            [Int]$Btn = 4
            Continue
        }
        'RetryCancel' {
            [Int]$Btn = 8
            Continue
        }
        default {
            [Int]$Btn = 0
            Continue
        }
    }

    #Affect icon value based on $Icon parameter selected
    switch ($Icon) {
        'Error' {
            [Int]$Ico = 16
            continue
        }
        'Question' {
            [Int]$Ico = 32
            continue
        }
        'Warning' {
            [Int]$Ico = 48
            continue
        }
        'Info' {
            [Int]$Ico = 64
            continue
        }
        default {
            [Int]$Ico = 0
            continue
        }
    }

    #Display the MessageBox and return the clicked button value
    $Reponse = [System.Windows.Forms.MessageBox]::Show($Message, $Title , $Btn, $Ico)

    if ($Btn -ne 0) {
        Write-Output -InputObject $Reponse
    }
}

Here is a small usage example, I use the following PowerShell
[PS]New-MessageBox -Message ‘Test Windows Form MessageBox with some random content’ -Title ‘MessageBox Random Title’ -Buttons OkCancel -Icon Info[/PS]
And here is screenshots of the resulting actions:

See you,