Interactive user input with Read-Host
Table of Contents
I. Introduction
The "Read-Host" cmdlet in PowerShell is invaluable, as it enables us to interact with the user by prompting him to enter information from the console. We can then store the value entered, i.e. the text entered, in a variable for use in a script. Whether you're asking for a password, a first and last name to create a login, etc... "Read-Host will do you a world of good!
II. Getting started with Read-Host
The simplest way to use "Read-Host" is to request user input without specifying a message, but we'll see that this isn't very intuitive. Ideally, the value entered by the user should be assigned to a variable so that it is not lost:
$Texte = Read-Host
By executing this code, PowerShell will wait for the user to enter something and press Enter to validate. What the user enters is then stored in the "$Text" variable.

It is also possible to provide the user with a message indicating the type of information requested. We'll simply indicate the message following the "Read-Host" command, which will be associated as a value with the "-Prompt" parameter.
Here is an example with two input requests:
$UtilisateurNom = Read-Host "Veuillez saisir votre nom"
$UtilisateurPrenom = Read-Host "Veuillez saisir votre prénom"
Write-Output "Nom : $UtilisateurNom - Prénom : $UtilisateurPrenom"
In this case, PowerShell will display the message "Please enter your name", then wait for the user to enter something. Then PowerShell will display the message "Please enter your first name" and wait for the user to enter a value. You don't need to add ":" at the end of your message for the prompt, as "Read-Host" adds it automatically.
Here's the result in pictures:

III. Entering a secure string
In some cases, we may use "Read-Host" to ask the user to enter a password. This value is a sensitive string, and we might not want the user's input to be directly visible. Furthermore, as this is a password, we'll be creating a "Secure String" rather than a simple string. "Read-Host" allows you to do this by using the "-AsSecureString" parameter.
Here's an example:
$MotDePasse = Read-Host "Veuillez saisir un mot de passe" -AsSecureString
Using this syntax, PowerShell will display the message "Please enter a password" and mask the user's input by displaying asterisks (*) instead of characters.

You can then verify that it's a secure string by checking the variable type. What's more, the value of the "$Password" variable is not directly accessible as plain text.
$MotDePasse.GetType()
We can then retrieve this value using the "ConvertFrom-SecureString" cmdlet and its "-AsPlainText" parameter, available with PowerShell 7. The command below will return the plaintext password, i.e. the one entered by the user via the "Read-Host".
$MotDePasse | ConvertFrom-SecureString -AsPlainText
IV. Conclusion
We've just seen how to use the "Read-Host" cmdlet to enable interactive user input. Although we don't need to do this in every script, we're happy to do it when the opportunity arises.
We could go a step further by coupling the use of "Write-Host" / "Write-Output" and "Read-Host" to create an interactive menu that would allow the user to make a choice, and depending on this choice, we could execute a specific action. This involves using a conditional structure ("if" or "switch") to act on the user's choice.