You can automate recurrings tasks with interactive PowerShell scripts.
How to query input values?
How to make PS scripts interactive?
An introduction to Read-Host, Confirm, PromptForChoice and Prompt
PowerShell scripts are popular to automate recurring tasks in the MS Windows environment:
The user starts a script, the script executes a task – and done!
- But what if the script is depending on decisions and information of users?
- Which folders should be backed up?
- What is the name of the new employee you want to create?
For this purpose, PowerShell provides some very powerful ways to interact with the user. These ways are described below.
Simple Queries with interactive PowerShell scripts
In the first section, I want to show you how to make simple input queries with PowerShell.
This means, that the script will stop – ask for an input – and continue its task after it received the input.
To query more than one information, please read more about multiple values below.
Simple Query with Read-Host
The easiest way to request data from a user is the PowerShell Cmdlet Read-Host.
$name = Read-Host 'What is your name?'
The result will look like this:
The user is prompted for a command and only after he has entered any text, the script execution continues. The entered string value is stored in the variable name. Read-Host doesn’t perform any input validation check. That means, Read-Host accepts empty input (the user only hits enter) or input that the script will not be able to deal with (eg, the script expects a number of users, but the input is a letter). Therefore, it is advisable to check the value obtained before further processing.
But Read-Host offers the possibility for secure queries.
$password = Read-Host 'What is your password?' -AsSecureString
The output will show only stars (*) instead of the real input to the user. This is pretty useful if you want people to enter passwords or security relevant information. Read-Host returns the value in a safe SecureString.
User confirmation with –Confirm
All Cmdlets that somehow have an impact on the system, offer the possibility to ask for a confirmation. For this purpose there is the parameter –Confirm.
Remove-Item empty.txt -Confirm:$Y
Depending on the Cmdlet the user will be asked to choose on of the following options[Y] Yes [A] Yes to all [N] No [L] No to all [S] Suspend [?] Help
In the example I set a preselection for yes. In case the user just hits the enter key the script will continue with „Y“ for Yes.
If you select no, the script will stop.
PromptForChoice – Let the user select
PromptForChoice provides multiple choice, includes the –Confirm parameter, is not limited to safety queries. And it is not difficult to adjust the queries…
$title = "Add employee?"
$message = "You want to add a new employee to your organisation?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Yes"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "No"
$options = [System.Management.Automation.Host.ChoiceDescription]($yes, $no)
$choice=$host.ui.PromptForChoice($title, $message, $options, 1)
The user has the option to select one of the choices. If he hits enter without any input, the pre-selection is accepted. PromptForChoice returns the field position (int) of the ChoiceDescription (index starts with 0) which was selected by the user. The example uses 2 choices, but there is no limit to the number.
Alternatively, it is possible to let the user select multiple values simultaneously.
$title = "Backup"
$message = "Please select resources for backup!"
$option1 = New-Object System.Management.Automation.Host.ChoiceDescription "Personal &Account", "Account"
$option2 = New-Object System.Management.Automation.Host.ChoiceDescription "Personal &Folder", "Folder"
$option3 = New-Object System.Management.Automation.Host.ChoiceDescription "Personal &Database", "Database"
$option4 = New-Object System.Management.Automation.Host.ChoiceDescription "&Public Folder", "Public"
$options = [System.Management.Automation.Host.ChoiceDescription]($option1, $option2, $option3, $option4)
$backup=$host.ui.PromptForChoice($title, $message, $options, [int](1))
And here is the output:
In this case PromtForChoice returns an array with the user-selected options. You can also set multiple options which will be pre-selected when you call. To this end, you have to specify a field with the positions of the pre-selected options (in this example only the second option is preselected).
Query Multiple Values at once with Prompt
A rarely used way to create interactive PowerShell scripts is to query several data at once.
For example the Login input from the beginning of this article.
Username and password belong together, so it is natural to query both information at once.
This is what Prompt is about.
$title = "Login"
$message = "Please enter your information to login!"
$name = New-Object System.Management.Automation.Host.FieldDescription "Name"
$name.Label = "&Login Name"
$name.DefaultValue = "Guest"
$pwd = New-Object System.Management.Automation.Host.FieldDescription "Password"
$pwd.Label = "&Password"
$pwd.SetparameterType( [System.Security.SecureString] )
$pwd.HelpMessage = "Please type your Password."
$fields = [System.Management.Automation.Host.FieldDescription]($name, $pwd)
$login=$Host.UI.Prompt($title, $message, $fields)
And here is the output:
The user is prompted for the required values in succession and the script execution continues only after all information is available. Prompt shows the returns the received input in a dictionary or hash table. For each retrieved entry you also get one table entry. The key is the name of the FieldDescription (in the example “Name” and “Password”) and the value of the data entered by the user.
You can get the latest Version of PowerShell if you download the Windows Management Framework.
If you have more ideas for user interactive Powershell scripts, please write a comment.