Store API Keys Safely in PowerShell

You have an API key and you need it in a script without typing it into the script. PowerShell’s answer is the SecretManagement module with the SecretStore vault. Set it up once:

PowerShell
Install-Module Microsoft.PowerShell.SecretManagement, Microsoft.PowerShell.SecretStore -Scope CurrentUser
Register-SecretVault -Name LocalStore -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault

Then save the key and read it back:

PowerShell
Set-Secret -Name AnthropicApiKey -SecureStringSecret (Read-Host 'API key' -AsSecureString)
Get-Secret -Name AnthropicApiKey -AsPlainText

Both modules are Microsoft’s, from the PowerShell Gallery, and work in Windows PowerShell 5.1 and PowerShell 7. -Scope CurrentUser installs them for your account only, so you do not need an admin window.

The Store Password

The first time you use the vault, SecretStore asks you to set a password for it. That password protects everything in the store. After you unlock it, it stays unlocked for a while and then locks again, and the next Get-Secret asks for the password. To see the settings:

PowerShell
Get-SecretStoreConfiguration

If a vault prompt is getting in your way, Unlock-SecretStore unlocks it for the session.

Use the Key Without Showing It

Most of the time you do not need to see the key, only to hand it to something. Load it into an environment variable for the current session:

PowerShell
$env:ANTHROPIC_API_KEY = Get-Secret -Name AnthropicApiKey -AsPlainText

Scripts and tools that read the variable pick it up from there. The variable is gone when the window closes, and the key stays in the vault.

Without -AsPlainText, Get-Secret returns a SecureString, which is what you want for commands that take a credential.

See What Is Stored

PowerShell
Get-SecretVault
Get-SecretInfo

Get-SecretInfo lists the names and types, not the values, so it is safe to run on a shared screen.

To replace a key, run Set-Secret again with the same name. To delete it:

PowerShell
Remove-Secret -Name AnthropicApiKey

Where Keys End Up by Accident

  • The script itself. Scripts get copied, shared and committed to Git, and the key goes with them.
  • Your profile. A line in your PowerShell profile runs in every session, and the file is plain text.
  • Your command history. Typing $env:ANTHROPIC_API_KEY = 'sk-ant-...' can save that line to the PSReadLine history file. Read-Host avoids it.

To check your history file for a key that is already there:

PowerShell
Select-String -Path (Get-PSReadLineOption).HistorySavePath -Pattern 'sk-ant'

Any line it finds is a key that has been sitting in a text file. Delete the line from the file and rotate the key in the provider’s console. Search Your PowerShell Command History has more on the history file.

Scripts That Run Unattended

A scheduled task cannot type the store password. SecretStore can be set to need no password:

PowerShell
Set-SecretStoreConfiguration -Authentication None -Interaction None

That swaps protection for convenience: anything running as your Windows account can then read the store without a prompt. It is still better than a key in the script, but think about which account the task runs as before you do it. How to Automate PowerShell Scripts With Task Scheduler covers setting up the task itself.

Related

If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *