PowerShell: Read Passwords Securely from Command Line

Some of the PowerShell commands require a password. You can embed a password directly in PowerShell commands. However, it is unwise to do it this way. It can be read by normal users and can be compromised. The best option is to store the password as an encrypted string in a text file. Even if a normal user reads it, he cannot guess the password. If a script or command needs the password, you can read the encrypted password string from the text file, convert it back, and use it.

This article illustrates how to read a password as a secure string from the command line, convert it to an encrypted string, and save it to a text file.

Save passwords securely with PowerShell

Step 1. Open PowerShell with elevated privileges

Step 2. Execute the following command.

Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File "C:\Users\securepassword.txt"

Leave a Comment