Print
Hits: 2164

The code below will let you store an encrypted password to disk for use in scripts.  The user used to create encrypted password must be the same user that will run the script.

Encrypt the password:

(Get-Credential).password | ConvertFrom-SecureString | set-content c:\scripts\cred.txt

The Encrypt password is now ready for use.  You can use it inside your code like this:

$user = 'domain\username'
$cred = New-Object System.Management.Automation.PsCredential $user,(Get-Content C:\scripts\cred.txt | ConvertTo-SecureString)

If you want to include a clear txt password in your script you can use the method below:

$user = 'domain\username'
$password
= ConvertTo-SecureString 'PlainTextPassword' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PsCredential $user, $password