If you’re authoring PowerShell scripts that are intended to be interactive, there are a couple of very easy ways to ask your users for input. Typically, user input falls into a couple categories: free-form text input, or a list of pre-defined values. We also might need to prompt the user to type in a secure credential.
User Input: Free-Form Text Input
If you need a user to provide input to your PowerShell script using free-form text, you can simply use the Read-Host command. For example, you can ask the user which computer they would like to establish a PowerShell Remoting session to.
User Input: Pre-defined Values
If you need to allow a user to pick one or more items from a set of pre-defined values, and don’t mind using UI windows, you can use the Out-GridView command in PowerShell 3.0 and later.
This command has a parameter called -OutputMode, which can be set to None, Single, or Multiple, depending on how many values you want the user to pick.
The Out-GridView command also offers a user-friendly -Title parameter that allows you to specify some text to help guide the user.
User Input: Credentials
You may also need to prompt your users for credentials. In this case, you do not want to use regular strings to store passwords!! Instead, you want to use what’s called a SecureString in .NET, which you can obtain using the Get-Credential command. The Get-Credential command returns a PSCredential object, which has a property called Password, which is of type SecureString. This helps to ensure that any passwords stored in memory are secured.
In this article we took a look at a few, simple ways to make your PowerShell scripts interactive!