Functions/Show-ListMenu.ps1

function Show-ListMenu
{
<#
.Synopsis
    Display a menu from a list of strings and return the selected object.
 
.DESCRIPTION
    Creates and displays a menu using a list of strings as menu items.
    When the user selects an item, returns the object at that same position
    from the objects array.
 
.PARAMETER MenuItems
    Array of strings to display as menu options.
 
.PARAMETER Objects
    Array of objects corresponding to each menu item. The selected item's
    return value will be the object at the same position as the selected string.
 
.PARAMETER MenuName
    Optional name for this menu display. Defaults to "ListMenu".
 
.PARAMETER Force
    Passed through to Show-Menu to suppress confirmation prompts.
 
.PARAMETER LastResult
    OPTIONAL. The result of the last executed menu item.
.PARAMETER Multi
    OPTIONAL. If set, allows multiple selections in the menu.
.EXAMPLE
    $items = @(
        [pscustomobject]@{Name=""; DisplayName="Option 1"; Value=1; DisableMultiSelect=$false},
        [pscustomobject]@{Name=""; DisplayName="Option 2"; Value=2; DisableMultiSelect=$false},
        [pscustomobject]@{Name=""; DisplayName="Option 3"; Value=3; DisableMultiSelect=$false},
        [pscustomobject]@{Name=""; DisplayName="Cancel"; Value=$null; DisableMultiSelect=$true}
    )
    $selectedUser = Show-ListMenu -MenuItems $items
 
.NOTES
    NAME: Show-ListMenu
    KEYWORDS: General scripting Controller Menu List
#>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]
        [pscustomobject[]]
        $MenuItems
        ,
        [string]
        $MenuName = "ListMenu"
        ,
        [switch]
        $Force
        ,
        [switch]
        $Multi
        ,
        [pscustomobject]
        $LastResult = @()
    )

    # Create a temporary menu with the items
    New-Menu -Name $MenuName -DisplayName $MenuName | Out-Null

    # Build a hashtable to store object references by item name
    $objectMap = @{}

    # Create menu items dynamically
    for ($i = 0; $i -lt $MenuItems.Count; $i++)
    {
        $itemName = "Item$i"
        # Store the object reference
        $objectMap[$itemName] = $MenuItems[$i].Value

        # Create action that returns the item name so we can look up the object
        $menuItem = New-MenuItem -Name $itemName -DisplayName $MenuItems[$i].DisplayName -DisableMultiSelect:$MenuItems[$i].DisableMultiSelect -DisableConfirm -Action {
            #need to fix this again
            param($menuItem)
            $menuItem.Name
        }

        Add-MenuItem -Menu $MenuName -MenuItem $menuItem
    }

    # Display menu and return the selected object
    try
    {
        $result = $LastResult
        while ($true)
        {
            $result = Show-Menu -MenuName $MenuName -Force:$Force -LastResult $result -Multi:$Multi

            if ($result -is [PSCustomObject] -and $result.menu)
            {
                if ($result.menu -eq "back" -or $result.menu -eq "main") { break }
                if ($result.menu -eq "exit") { exit }
            }
            elseif ($result -is [string] -and $objectMap.ContainsKey($result))
            {
                # Return the object at the selected index
                $selectedObject = $objectMap[$result]
                break
            }
            elseif ($result -is [array] -and $Multi)
            {
                $selectedObjects = @()
                foreach ($itemName in $result)
                {
                    if ($objectMap.ContainsKey($itemName))
                    {
                        $selectedObjects += $objectMap[$itemName]
                    }
                }
                if ($selectedObjects.Count -gt 0)
                {
                    $selectedObject = $selectedObjects
                    break
                }
            }
             else
            {
                Write-Warning "Unexpected menu result: $result"
            }
        }
    }
    finally
    {
        # Clean up: remove all menu items
        foreach ($itemName in $objectMap.Keys)
        {
            Remove-MenuItem -MenuName $MenuName -ItemName $itemName -ErrorAction SilentlyContinue
        }

        # Delete the menu
        $menuToRemove = $script:Menus | Where-Object { $_.Name -eq $MenuName }
        if ($menuToRemove)
        {
            $null = $script:Menus.Remove($menuToRemove)
        }
    }

    # Return the selected object
    if ($selectedObject)
    {
        Write-Output $selectedObject
    }
}