October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Create a User Menu in a PowerShell Script

Use Read-Host to collect a menu choice and switch to run its action. Add a loop, a default branch, and a quit option for a reusable console menu.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a simple interactive PowerShell menu, display the choices, collect a response with Read-Host, and use switch to run the matching action. Put the menu in a loop to show it again after each action, handle unexpected input with default, and give users a clear option to quit.

Build a basic repeating menu

This example offers two actions and a quit option. It trims accidental spaces from the response, returns to the menu after an action, and gives an error message for anything it does not recognize.

do {
    Clear-Host
    Write-Host "Choose an option:"
    Write-Host "1. Show date"
    Write-Host "2. List files"
    Write-Host "Q. Quit"

    $choice = (Read-Host "Enter your choice").Trim()

    switch ($choice) {
        '1' {
            Get-Date
            Read-Host "Press Enter to return to the menu" | Out-Null
        }
        '2' {
            Get-ChildItem
            Read-Host "Press Enter to return to the menu" | Out-Null
        }
        'Q' {
            Write-Host "Goodbye."
        }
        default {
            Write-Host "Choose 1, 2, or Q."
            Read-Host "Press Enter to try again" | Out-Null
        }
    }
} until ($choice -eq 'Q')

The menu arrangement is a coding pattern built from PowerShell’s input, branching, and looping features; there is no built-in menu command in this example. Microsoft documents Read-Host as reading a line of input from the console, while switch maps matching values to action blocks.

How input and choice matching work

Read-Host returns text

By default, Read-Host returns a string, so the sample compares the response with quoted values such as '1'. Convert the response to a number only when you need numeric operations or numeric validation; a menu selection usually needs only a match against known options. The PowerShell 7.5 documentation specifies a maximum input length of 1022 characters.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
PowerShell for Sysadmins: Workflow Automation Made Easy
  • Book - powershell for sysadmins: workflow automation made easy
  • Language: english
  • Binding: paperback

switch handles recognized and unrecognized entries

Each matching switch clause runs its action block. The default clause handles a response that matches none of the listed choices. Without options such as -Regex or -Wildcard, PowerShell’s switch uses exact, case-insensitive matching; consequently, Q and q both select the quit action here.

The loop controls whether the menu returns

The do block runs the menu and its selected action, then the until condition checks whether the user selected Q. After any other choice—including invalid input—the loop repeats. The pause after an action or error lets the user read the output before the next screen is cleared.

Adapt the menu safely

  • Change the displayed choices, matching labels, and validation message together so that users can see exactly which entries are accepted.
  • Keep short actions in the menu; move longer work into named functions and call those functions from the relevant switch block.
  • Validate a choice before running work that depends on it. Microsoft’s PowerShell 101 functions guidance advises not letting code continue along a path when it cannot complete without valid input.
  • For a function that receives a choice as a parameter, validation attributes can reject invalid submitted values; Microsoft documents that the function is not called when such a value is invalid. This parameter validation is separate from checking a response collected with Read-Host. See Advanced function parameters for Windows PowerShell 5.1.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose an input method that fits the script

A Read-Host menu is for a person interacting with a console. Since it waits for keyboard input, it is unsuitable for unattended jobs unless the script is redesigned to receive a parameter or another noninteractive input source. Microsoft’s training module on accepting input in Windows PowerShell scripts covers distinct options including Read-Host, Get-Credential, Out-GridView, and script parameters; choose based on whether the task needs a console prompt, graphical selection, or invocation-time input. It does not establish a direct feature-by-feature comparison among those methods.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.