Skip to main content

Read a Line of User Input in Windows PowerShell

Problem

You want to use input from the user in your script.

Solution

To obtain user input, use the ReadHost cmdlet:

PS >$directory = ReadHost "Enter a directory name" Enter a directory name: C:\MyDirectory PS >$directory C:\MyDirectory

Discussion

The ReadHost cmdlet reads a single line of input from the user. If the input contains sensitive data, the cmdlet supports an –AsSecureString parameter to read this input as a SecureString.

If the user input represents a date, time, or number, be aware that most cultures represent these data types differently.

For more information about the ReadHost cmdlet, type GetHelp ReadHost.

A US Dedicated Server provides an isolated environment for Windows-based workloads with control over the operating system, server configuration, and dedicated resources.

Help Category