Skip to content

Instantly share code, notes, and snippets.

@sean-mcardle
Created February 18, 2025 19:28
Show Gist options
  • Save sean-mcardle/c9866c42089b3e48a2bfe2ab9ec8c01a to your computer and use it in GitHub Desktop.
Save sean-mcardle/c9866c42089b3e48a2bfe2ab9ec8c01a to your computer and use it in GitHub Desktop.
function Out-SelectorMenu {
param (
[Parameter(ValueFromPipeline=$true)]
$InputObject,
[Parameter(Position=0)]
$Title
)
begin {
$_items = @()
}
process {
if ($InputObject) {
$_items += $InputObject
}
}
end {
Write-Host $Title
Write-Host ($_items | ForEach-Object {$index=0} {$_; $index++} | Format-Table @{Label="Index";Expression={$index}; Width=5 }, * | Out-String)
$selectionMade = $false
$selection = $null
do{
$selection = Read-Host -Prompt "Make a selection by index ('c' to cancel)#"
if ($selection -match '[0-9]+' -and [int]$selection -ge 0 -and [int]$selection -lt $_items.Count) {
$selectionMade = $true
break
} elseif ($selection -and $selection.Trim() -like 'c') {
break
}
} while ($true)
if ($selectionMade) {
return $_items[$selection] | Select-Object -ExcludeProperty 'Index'
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment