Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Created February 10, 2025 20:01
Show Gist options
  • Select an option

  • Save steviecoaster/60cbc0685fdf1c2bdd1e99f24f13b470 to your computer and use it in GitHub Desktop.

Select an option

Save steviecoaster/60cbc0685fdf1c2bdd1e99f24f13b470 to your computer and use it in GitHub Desktop.

Revisions

  1. steviecoaster created this gist Feb 10, 2025.
    39 changes: 39 additions & 0 deletions New-UserPrompt.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    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)]
    }
    }
    }