Need Input? Read-Host

PowerShell scripts can perform all sorts of functions. They can gather information, make reports, change settings and prepare servers for app installs. The scripts can be run as scheduled tasks, be triggered by events or run at a PowerShell console manually. Some scripts do not need input. But what if your script does? How can your PowerShell script prompt for input?

[sourcecode language=”powershell”]
Read-Host
[/sourcecode]
Below are some use cases as well as how to use the relatively few parameters provided by the cmdlet:

Password prompt and secure storage

Method One

For this method, a sentence will be written to the PowerShell console with Write-Host to be followed-up with a Read-Host statement.
[sourcecode language=”powershell”]
Write-host "Enter O365 Login Name (UPN): " -ForegroundColor White -NoNewLine
$SMTPusername = Read-Host
[/sourcecode]
Notice that the Write-Host has a ‘-NoNewLine’ at the end, this means that user input will occur right after the question is asked:

The user name is also now stored in a variable to be used later in a script. The value is also stored as a string.

Method two

For this method, we need to capture a password to be used to pass along to another system for a connection or access to a particular workload. We can use Read-Host to securely store this password. We need to apply a switch ‘AsSecureString’, but it is possible. So we can use code like this:
[sourcecode language=”powershell”]
Write-host "Please enter a password for the admin account: " -NoNewLine -ForegroundColor Yellow
$Pwd = Read-Host -AsSecureString
[/sourcecode]
Just the same as Method one, we can enter the password on the same line as the question prompt in the console. However, notice that the when characters are typed, they are replaced with an asterix:

Now the password is stored in a secure variable, to be retrieved later for some sort of authentication process.

Method Three

For this last method, we’ll ask for a password, but instead of just storing it in a password, we’ll also write the value to a file as well:
[sourcecode language=”powershell”]
Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString | Out-File "c:\Password.txt"
[/sourcecode]
Notice that we are not storing the initial Read-Host as a variable, but writing it straight to a file:

This then is stored in a file which looks like this:

Now this file can be pulled into a variable and used by a scheduled script like so:
[sourcecode language=”powershell”]
$SMTPpassword = Cat "c:\Password.txt" | ConvertTo-secureString
[/sourcecode]
The downside to this is that this is obviously a security risk. This file should be stored in a secure location and not accessible to other people.

General decision making

Read-Host can be used as a quick way to have the operator enter a decision or option to have the script perform a specific task. This could be from a menu or just general input needed to feed a particular function.

For a menu, the operator is provided a few choices like so:
[sourcecode language=”powershell”]
$Menu = {
Write-Host " 1) Install Mailbox Role Prerequisites – Full OS" -ForegroundColor White
Write-Host " 2) Install Mailbox Role Prerequisites – Core OS" -ForegroundColor White
Write-Host " 3) Install Edge Transport Prerequisites – Full OS / Core OS" -ForegroundColor White
Write-Host " Select an option.. [1,2 or 3]? " -ForegroundColor White -nonewline
}
$Choice = Read-Host
[/sourcecode]
The operator now has a choice of 1,2 or 3 in order to perform some sort of function in the script.

Simplifying the above

In a lot of the above examples, we’ve used Write-Host to prompt for the entry of some operator input. We can simplify this by skipping the Write-Host cmdlet and using the ‘-Prompt’ parameter like so:
[sourcecode language=”powershell”]
Read-Host -Prompt "Enter your user name here"
[/sourcecode]

OR
[sourcecode language=”powershell”]
Read-Host -Prompt "Enter a password" -AsSecureString
[/sourcecode]

Related Post

CIM to WMICIM to WMI

Years ago WMI was de-emphasized for CIM. Wait. Do you know what those are? CIM – “The Common Information Model (CIM) is an extensible, object-oriented data model that contains information