Prof. Powershell

Do You Read Me?

Sans GUI, PowerShell can be made to work interactively. The trick is the Read-Host cmdlet.

PowerShell 1.0 is meant to be used as a management console. As such, it doesn't have a graphical interface. There are no input or message boxes like those in VBScript, although those items can be created through the use of WinForms or through third-party snap-ins. But that doesn't mean you can't have an interactive script. If your scripts require interactive input, use the Read-Host cmdlet:

PS C:\> read-host "Enter a computername"
Enter a computername: SRV03
SRV03
PS C:\> 

The only parameter the cmdlet requires is a prompt. The cmdlet takes the user's input and writes it to the pipeline. In this example, it essentially went back to the screen. You'll most likely want to save the information to a variable:

PS C:\> $c=read-host "Enter a computername"
Enter a computername: SRV03
PS C:\> $c
SRV03
PS C:\> get-wmiobject win32_bios -computername $c

There's no real limit to the size of the string. You can even use the tab and spacebar for simple formatting.

PowerShell will end the string when you press Enter. I like using Read-Host as a Pause substitute:

PS C:\> read-host "press enter to continue"
press enter to continue:
PS C:\> 

If I use this in a script, PowerShell will wait until I press Enter before continuing with the rest of the script. When I was writing batch files, I used Pause quite a bit, and it's what I use to get the same result in PowerShell.

By default the Read-Host cmdlet uses simple strings. But you can also use the -asSecureString parameter, which instructs the cmdlet to convert the entered string as a secure string:

PS C:\> $pwd=read-host "Enter a password 
for the new user"  -asSecureString
Enter a password for the new user: ***********
PS C:\> $pwd
System.Security.SecureString

That parameter also instructs the cmdlet to mask the entered string. I can now use $pwd in any cmdlet or PowerShell expression that requires a secure string.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

Featured

  • Azure Active Directory Connect Preview Adds Support for Disconnected AD Forests

    Microsoft on Thursday announced a preview of a new "Cloud Provisioning" feature for the Azure Active Directory Connect service that promises to bring together scattered Active Directory "forests."

  • Microsoft Defender ATP Gets macOS Investigation Support

    The endpoint and detection response (EDR) feature in Microsoft Defender Advanced Threat Protection (ATP) has reached the "general availability" stage for macOS devices.

  • How To Block Self-Service Purchasing in Microsoft's Power Platform

    Microsoft threw Office 365 admins a bone when it gave them the ability to block users from purchasing Power Platform tools without IT approval. Here's how to prevent total anarchy.

  • Azure DevOps Services Losing Support for Alternate Credentials

    Microsoft gave notice last week that it's going to drop Alternate Credentials support for authenticating users of its Azure DevOps Services.

comments powered by Disqus

Office 365 Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.