How to Prompt for Variable in PowerShell?

Someone from the team asked about the PowerShell prompt for variables. We can do this by using the Read-Host cmdlet. In this tutorial, let me explain the PowerShell prompt for a variable with some examples.

To prompt for a variable in PowerShell, use the Read-Host cmdlet. The basic syntax is $variable = Read-Host "Prompt message", where “Prompt message” is the text displayed to the user. For example, $name = Read-Host "Please enter your name" will store the user’s input in the $name variable, making your script interactive and adaptable to user input.

PowerShell Prompt for Variable

In PowerShell, instead of hardcoding values, you can ask the user to input necessary data at runtime, and that is when we are required to prompt a variable.

Read-Host Cmdlet

The best and most recommended way to prompt for user input in PowerShell is using the Read-Host cmdlet. This cmdlet reads a line of input from the console and stores it in a variable.

Syntax

Here is the syntax:

$variable = Read-Host "Prompt message"

Example

Let’s say we need to prompt for a user’s name and age in a PowerShell script.

$name = Read-Host "Please enter your name"
$age = Read-Host "Please enter your age"

Write-Output "Hello, $name! You are $age years old."

In this example, $name and $age are variables that store user input. The Write-Output cmdlet then prints the message to the console.

I executed the above PowerShell script using VS code, and you can see the exact output in the screenshot below:

powershell prompt for variable

Check out PowerShell If Variable Contains

Prompt for Secure Input in PowerShell

Sometimes, you need to prompt for sensitive information like passwords. For this, you can use the -AsSecureString parameter with Read-Host.

Let me show you an example.

Example

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

Write-Output "Password entered successfully."

The -AsSecureString parameter ensures that the input is masked and stored securely.

PowerShell Prompts with Validation

You might want to validate user input to ensure it meets certain criteria. You can achieve this by using a loop that continues to prompt the user until the input is valid.

Let me show you an example to help you understand.

Example

do {
    $age = Read-Host "Please enter your age (must be a number)"
} while (-not ($age -match '^\d+$'))

Write-Output "You entered a valid age: $age"

In this script, the loop keeps prompting the user until a numeric value is entered.

You can see the exact output in the screenshot below:

Prompt for Variable in PowerShell

Read PowerShell Private Variables

Using GUI Prompts in PowerShell

Graphical user interface (GUI) prompts can provide a more user-friendly experience, especially for non-technical users. PowerShell can leverage .NET classes to create simple GUI dialogs.

Here is an example.

Example

Add-Type -AssemblyName Microsoft.VisualBasic
$name = [Microsoft.VisualBasic.Interaction]::InputBox("Please enter your name", "Name Prompt", "John Doe")

Write-Output "Hello, $name!"

This script uses the InputBox method from the Microsoft.VisualBasic namespace to create a GUI prompt.

PowerShell Prompt for Variable: Examples

Now, let me show you some real examples of how to prompt for a variable in PowerShell.

Example 1: Create a New User

Imagine you are creating a script to add new users to your Active Directory. You can prompt for details like username, password, and email.

Here is the PowerShell script.

$username = Read-Host "Enter the new user's username"
$password = Read-Host "Enter the new user's password" -AsSecureString
$email = Read-Host "Enter the new user's email"

# Example command to create a new user (simplified)
New-ADUser -Name $username -AccountPassword $password -EmailAddress $email

Check PowerShell Static Variables

Example 2: Configure Network Settings

If you’re writing a script to configure network settings, you might prompt for IP address, subnet mask, and gateway.

Here is the PowerShell script.

$ipAddress = Read-Host "Enter the IP address"
$subnetMask = Read-Host "Enter the subnet mask"
$gateway = Read-Host "Enter the default gateway"

# Example command to set network configuration (simplified)
Set-NetIPAddress -IPAddress $ipAddress -PrefixLength $subnetMask -DefaultGateway $gateway

Conclusion

In this tutorial, I explained how to prompt for variables in PowerShell using the Read-Host cmdlet. I have explained different scenarios and examples.

I hope this tutorial helps you understand how to prompt for variables in PowerShell. Feel free to let me know in the comment below if you have any questions.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.