For more technical explainers on PowerShell, read our 2021 PowerShell 101: A Technical Explainer for IT Pros report.
Q: I’m just getting started with PowerShell. I know how to make variables and assign values to them within a script but how can I prompt for user input and then assign that input to a variable?
A: You can prompt for user input with PowerShell by using the Read-Host cmdlet. The Read-Host cmdlet reads a line of input from the PowerShell console. The –Prompt parameter enables you to display a string of text. PowerShell will append a colon to the end of the string. Users can enter input after the colon. The following code snippet shows how you can prompt for user input and then assign that input to two variables named $Server and $User.
$Server = Read-Host -Prompt 'Input your server name' $User = Read-Host -Prompt 'Input the user name' $Date = Get-Date Write-Host "You input server '$Servers' and '$User' on '$Date'"
ITPro Today has a growing library of PowerShell explainers and scripts. Here's a sampling of articles:
How To Use Automatic PowerShell Transcription: Use transcription for security logging and troubleshooting.
How to Encrypt PowerShell Scripts: Use encryption to protect sensitive PowerShell scripts.
How To Use PowerShell To Identify Corruption on NFTS Volumes: Spot corruption on NFTS volumes so you can prevent data loss events.
How To Use a For Each Loop in PowerShell: For Each loops let you perform actions against multiple items.
Use PowerShell To Find Password-protected Word Documents: Use PowerShell to find elusive files on your network.