Powershell Script to Encrypt a Password for Another Script to Use.

Simple Script that will create a text file that another scheduled script can then use to as the password for some account.  Example, you want a script to auto run and remote into some domain, machine, etc but you want to secure the password used by the script.  So it will not be stored in plain text on a server in a script file.  This will get that done.

You call this script from commandline once and then put the text file “securestring.txt” in a call in the automated script.

1. Create a file called encryptpassword.ps1 and put the following in it.

write-host "Enter password to encrypt "
$secure = read-host -assecurestring
$encrypted = convertfrom-securestring -secureString $secure | set-content securestring.txt
$secure2 = get-content securestring.txt | convertto-securestring
write-host "password decrypted is " $secure2

2. Open a powershell command prompt and launch .\encryptpassword.ps1

  • Type your password when prompted
  • Look in the directory you launched powershell from and you should see a securestring.txt

3. Then in your script call the securestring.txt

$username = "domain\user"
$password = get-content d:\powershellScripts\securestring.txt | convertto-securestring
$livecrd = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s