Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Created February 10, 2025 20:01
Show Gist options
  • Save steviecoaster/60cbc0685fdf1c2bdd1e99f24f13b470 to your computer and use it in GitHub Desktop.
Save steviecoaster/60cbc0685fdf1c2bdd1e99f24f13b470 to your computer and use it in GitHub Desktop.
Create a simple user prompt
function New-UserPrompt {
<#
.SYNOPSIS
Generate a simple user prompt
.DESCRIPTION
Long description
.PARAMETER Options
An array of choices a user can select from
.EXAMPLE
New-UserPrompt -Options 'Larry','Curly','Moe'
#>
[CmdletBinding()]
Param(
[Parameter()]
[String[]]
$Options
)
end {
$x = 1
foreach($o in $Options){
'{0}. {1}' -f $x,$o
$x++
}
[int]$prompts = $x -1
$choice = Read-Host -Prompt "Select option (1-$prompts)"
if([int]$choice -gt $prompts){
throw "Invalid option. Please choose between 1 and $prompts!"
} else {
$Options[($choice - 1)]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment