Tag Archives: functions

PowerShell Function Get-MenuInput

I was recently working on a script with a unique requirement.  There was a desire to automate a business process, but there were also a number of exceptions and special cases.  In the end we decided to try and filter/select the best options then present them to the user running the script and have them make the final decision.

As part of that script I created a handy little function that accepts an array virtually any length and collects the users input as to which one we should use.  For example you could pass a list of all Lync Registrar pools in North America and get input as to which one we should use for provisioning.

Feel free to use or adapt this as needed, just leave a comment below if you find it useful or see any improvements.

function Get-MenuInput {
        Allow the user to choose an option from a menu.
        This function prints a menu list to the screen based on a supplied array.
        It allows the user to choose and option based on
        the number index and returns the chosen value as a string.
        $Pool = Get-MenuInput -Array (Get-CsService -Registrar).PoolFqdn
        Get-MenuInput -Array $Array -Prefix "DialPlan:"
        .PARAMETER Array
        String Array of available menu options.
        .PARAMETER Prefix
        Text string to add to the front of each options.
        Returns the single chosen array item
    param (
        [Parameter (Mandatory = $true)]

    #Note how many objects are in the array.
    #Number of menu options
    $ArrayLength = $Array.Length

    #Print each option in the array with a numbered choice index
    #Do While Loop is used to validate the users input
    Do {
        #Reset the Numbered Choice Index
        $i = 1

        #Write a line to the host for each item
        foreach ($item in $Array) {
            Write-log -Level Info -Data "$i - $Prefix$item"

            #Increment the Choice Index

        #Save the users choice as a variable
        $MenuChoice =  Read-Host -Prompt 'Please Choose the number of the desired option' 

    } While ($MenuChoice -lt 1 -or $MenuChoice -gt $ArrayLength)

    #Decrease the variable by one to convert to an array index that starts with 0
    $MenuChoice = $MenuChoice - 1

    #Log the choice
    Write-Host "$($Array[$MenuChoice]) Chosen." -ForegroundColor Green

    #Return the chosen option as a string


As always this is provided AS IS with no warranty or guarantee.  Use at your own risk.