Sleep or Pause to PowerShell Script

Last Updated on August 7, 2025 by Arnav Sharma

PowerShell is a powerful scripting language developed by Microsoft, designed for task automation and configuration management. One of the fundamental features of PowerShell is its ability to prompt users for input, enabling scripts to be interactive and versatile. In this blog, we will explore how to use the read-host cmdlet to prompt users for input in PowerShell, including various methods, syntax, and practical examples.

PowerShell Prompt

What is read-host?

The read-host cmdlet in PowerShell is used to read a line of input from the console. It allows scripts to prompt the user for input, making it an essential tool for interactive scripts. The input typed by the user is stored as a plaintext string object, which can then be used within the script.

Using the read-host Cmdlet

The basic syntax of the read-host cmdlet is straightforward. To prompt the user for input, you use the read-host -prompt parameter, which specifies the text displayed to the user at the prompt.

$input = Read-Host -Prompt "Please enter your name"

Write-Host "Hello, $input!"

In this example, PowerShell will prompt the user with “Please enter your name:”. The user’s input is stored in the $inputvariable and then displayed back to the user with Write-Host.

Prompt for User Input

When you use the -Prompt parameter, PowerShell appends a colon (:) to the end of the prompt text. This provides a user-friendly indication that the script is waiting for input.

$name = Read-Host -Prompt "Enter your name"

Write-Host "Your name is $name"

Secure String Input

To prompt the user for sensitive information such as passwords, you can use the -AsSecureString parameter with read-host. This ensures that the input is stored as a secure string, which is more secure than a plaintext string.

$password = Read-Host -Prompt "Enter your password" -AsSecureString

Write-Host "Password entered."

In this case, the user’s input is not displayed on the console, providing a layer of security for sensitive data.

Input Validation

You can also validate the user’s input to ensure it meets specific criteria. Hereโ€™s an example of how to prompt for and validate user input:

do {
    $age = Read-Host -Prompt "Enter your age"
    if ($age -match '^d+$') {
        Write-Host "You entered a valid age: $age"
        $valid = $true
    } else {
        Write-Host "Invalid input. Please enter a valid number."
        $valid = $false
    }
} while (-not $valid)

In this script, the user is repeatedly prompted for their age until they enter a valid number, utilizing input to a variable in PowerShell.

Example Scenarios

Prompting for Multiple Inputs

You can prompt the user for multiple inputs within the same script. Hereโ€™s an example:

$name = Read-Host -Prompt "Enter your name"

$age = Read-Host -Prompt "Enter your age"

Write-Host "Your name is $name and you are $age years old."

Using Input in a Switch Statement

PowerShell allows you to use user input within a switch statement to control the flow of the script, highlighting user input with PowerShell.

$choice = Read-Host -Prompt "Would you like to continue? (yes/no)"
switch ($choice) {
    "yes" { Write-Host "You chose to continue."; }
    "no"  { Write-Host "You chose to exit."; }
    default { Write-Host "Invalid choice. Please try again."; }
}

Storing User Input

You can store user input with PowerShell in variables for later use in your script. For example, you might prompt the user for a start date and an end date, then use those values in a report.

$startDate = Read-Host -Prompt "Enter the start date (MM/DD/YYYY)"
$endDate = Read-Host -Prompt "Enter the end date (MM/DD/YYYY)"
Write-Host "Generating report from $startDate to $endDate..."
# Add your report generation code here

Using read-host for GUI Prompts

Although read-host is primarily used in the command-line interface, you can also create a more graphical user interface (GUI) prompt using the Windows Forms class.

Add-Type -AssemblyName Microsoft.VisualBasic
$username = [Microsoft.VisualBasic.Interaction]::InputBox("Enter your username", "User Input", "DefaultUser")
Write-Host "You entered: $username"

This script creates a popup input box for the user to enter their username, making it more user-friendly for non-technical users.


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.