Created
February 10, 2025 20:01
-
-
Save steviecoaster/60cbc0685fdf1c2bdd1e99f24f13b470 to your computer and use it in GitHub Desktop.
Create a simple user prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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