Pages

October 25, 2013

Embedding Encrypted Passwords in Powershell Scripts



Description: Use the following steps to convert a password into an encrypted string that can be embeded in a powershell script.

1. Use the following commands to create the encrypted string. Note: the encrypted string output is based on the user that is logged in so you will have to run these commands as the service account you are using if the service account is going to be running your script.
  • $password = ""
  • $secure = ConvertTo-SecureString $password -force -asPlainText
  • $encrypted = ConvertFrom-SecureString $secure
  • write-host $encrypted

2. Copy the encrypted string above into your powershell script. 
  • $encrypted = "01000000d08c9ddf0115d1118c7a00c04fc2......................................"

3. Now convert that string to a secure string in your powershell script
  • $password = ConvertTo-SecureString -String $encrypted

4. Create a new credential object
  • $cred = New-Object System.Management.Automation.PSCredential $username, $password

5. Now you can use the new credential object in your script.
example:  Invoke-Command -credential $cred -computername "" -scriptblock {get-host}