[Powershell]Obfuscate password

This is a sample script to create a password file, the file is encrypted, however when you need to read from the files you can use it for your script to authenticate so that your script do not need to hardcode the password in plaintext.

$username = "Your_Username"
if ((Test-Path .\password.do) -eq $true) {
    $password = Get-Content ".\password.do" | ConvertTo-SecureString
    $credential = New-Object System.Management.Automation.PSCredential($username,$password)
    } else {
        Read-Host $username -AsSecureString | ConvertFrom-SecureString | Out-File .\password.do
        $password = Get-Content .\password.do | ConvertTo-SecureString
        $credential = New-Object System.Management.Automation.PSCredential($username,$password)

The credential.GetNetworkCredential().Password will display your encrypted password in plaintext.

This is how password.do looks like:


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 )

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