PowerShell: Storing Passwords for Scripts with PSCredential

The Problem

A scheduled script needs to authenticate: send an email, hit an API, mount a share. Hardcoding the password in the script is how credentials end up in a git repository, and it is the single most common cause of leaked service credentials.

PowerShell has a built-in answer, and it is better than plaintext — but it comes with an important limitation that most guides skip, and that limitation is the reason people get stuck when they move the script to another machine.

A photorealistic shot of a laptop showing a PowerShell console prompting for a password with masked characters, a notebook and pen on the desk, warm lamp light, realistic photography, no legible text.

The Basic Pattern

Create the encrypted file once, interactively:

Read-Host 'Input Password' -AsSecureString |
    ConvertFrom-SecureString |
    Out-File .\SecurePassword.txt

The password is prompted, never echoed, never written in plaintext. What lands in the file looks like a long hex string.

Consume it in your script:

$UserName   = '[email protected]'
$Password   = Get-Content .\SecurePassword.txt | ConvertTo-SecureString
$Credential = New-Object System.Management.Automation.PSCredential($UserName, $Password)

$Credential is now usable anywhere a -Credential parameter is accepted.

The Limitation Nobody Mentions

ConvertFrom-SecureString without -Key uses DPAPI, which encrypts the blob against the current user account on the current machine.

That means the SecurePassword.txt you created on your workstation:

  • Cannot be read by a different user account
  • Cannot be read on a different machine
  • Cannot be read by a scheduled task running as SYSTEM
  • Is lost entirely if the machine is rebuilt

This is why the pattern "worked on my PC" and fails silently in production. The error is usually ConvertTo-SecureString : Key not valid for use in specified state, which tells you nothing about the cause.

If you need it portable, supply your own key:

# once: create and store a 32-byte AES key somewhere you control
$key = New-Object byte[] 32
[Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($key)
$key | Out-File .\aes.key

# encrypt with the key
Read-Host 'Password' -AsSecureString |
    ConvertFrom-SecureString -Key (Get-Content .\aes.key) |
    Out-File .\SecurePassword.txt -Encoding ascii

# decrypt with the key
$Password = Get-Content .\SecurePassword.txt |
            ConvertTo-SecureString -Key (Get-Content .\aes.key)

Now the blob is portable, and the key becomes the secret you must protect — which is the same problem one level removed. A key file sitting next to the encrypted file protects nothing.

What This Actually Buys You

Be clear about the threat model, because "encrypted password" sounds stronger than it is.

It protects against casual shoulder-surfing, a copy-pasted script in a chat, and a repository commit. The encrypted blob is not the password.

It does not protect against anyone who can run code as that user on that machine. They can call ConvertTo-SecureString exactly as your script does. This is obfuscation with extra steps for a local attacker, and it is genuinely useful against the "credentials in a text file in git" failure mode.

It does not rotate. A password stored this way in 2019 is still that password. If it leaks elsewhere, nothing here notices.

Better Options, In Order

Run the task as SYSTEM and use machine-level authentication instead of a user password. No stored secret at all.

A managed identity or app registration for anything talking to a cloud API. Client credentials with a certificate, rotated automatically.

Windows Credential Manager, via the CredentialManager module, keeps secrets in the OS vault rather than a file beside your script:

Install-Module CredentialManager -Scope CurrentUser
New-StoredCredential -Target 'MailRelay' -UserName 'luke' -Pass 'secret' -Persist LocalMachine
$cred = Get-StoredCredential -Target 'MailRelay'

A real secret store — HashiCorp Vault, Azure Key Vault, AWS Secrets Manager — if this is anything more than a home script.

Keep SecurePassword.txt and any aes.key out of source control regardless. Add them to .gitignore before you create them, not after.

A photorealistic close-up of a code editor showing a .gitignore file with credential filenames listed, a terminal window beside it, muted office lighting, realistic photography, no legible text.

A photorealistic shot of a Windows Credential Manager window listing generic credentials, a PowerShell window beside it, office desk with a mug, daylight, realistic photography, no legible text.

One Note On The File

Out-File in Windows PowerShell writes UTF-16LE by default, which ConvertTo-SecureString reads back fine but other tools find awkward. Use -Encoding ascii for the encrypted blob so it stays a single clean line of hex, and keep line endings consistent if the file ever moves between systems.

Don't trust the download button. Trust the hash.

If this was useful, More hands-on tech guides at TechGeek. No pressure — read it whenever it's useful.


Luke Keam
Spot a bug or want a custom utility built? I'd love to hear from you. Get in touch
Luke Keam | techgeek.biz