A PowerShell script for storing, retrieving, and removing secrets using your choice of storage provider — no third-party dependencies.
| Provider | Description | Platform notes |
|---|---|---|
Keyring (default) |
OS-native secure credential store | Windows: Credential Manager · macOS: Keychain · Linux: GNOME Keyring (secret-tool) |
Environment |
OS environment variable | Windows: User scope (persistent) · macOS/Linux: Process scope (current session only) |
File |
UTF-8 .env-style file (KEY=VALUE) |
Cross-platform; pass -FilePath or be prompted |
-
PowerShell 5.1+ (Windows) or PowerShell 7+ (macOS / Linux)
-
Linux Keyring provider only:
secret-toolfrom thelibsecret-toolspackagesudo apt install libsecret-tools # Debian / Ubuntu sudo dnf install libsecret # Fedora
Note (Linux headless servers):
secret-toolrequires a running keyring daemon (GNOME Keyring or KWallet). It will fail on servers without an active desktop session.
Use -Set with masked input (recommended), or pass the value directly via -Value.
# Prompted masked input — checks if key exists before asking for the value
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Set
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Set -Provider Environment
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Set -Provider File -FilePath '~/.secrets'
# Inline value
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Value 's3cr3t'
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Value 's3cr3t' -Provider Environment
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Value 's3cr3t' -Provider File -FilePath '~/.secrets'If the key already exists, you will be prompted to confirm before entering the value:
Secret 'MyApiKey' already exists. Override? [y/N]:
Pressing Enter (or N) skips — without ever prompting for the value.
# Silently do nothing if the key already exists (no value prompt)
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Set -IfExists Skip
# Silently overwrite — go straight to the value prompt
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Set -IfExists Override
# Same options work with -Value
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Value 's3cr3t' -IfExists Override# OS keyring (default)
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Get
.\Manage-Secret.ps1 -KeyName 'MyApiKey' # -Get is optional
# Environment variable
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Get -Provider Environment
# File
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Get -Provider File -FilePath '~/.secrets'
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Get -Provider File # prompts for pathOutput is written to the pipeline, so it can be captured or piped:
$token = .\Manage-Secret.ps1 -KeyName 'MyApiKey' -Get
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Get | Set-ClipboardIf the key does not exist, a terminating error is raised and the exit code is set to 1.
# Prompts for confirmation (default answer: No)
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Remove
# Skip the prompt and remove immediately
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Remove -Force
# Remove from a specific provider
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Remove -Force -Provider Environment
.\Manage-Secret.ps1 -KeyName 'MyApiKey' -Remove -Force -Provider File -FilePath '~/.secrets'| Parameter | Type | Required | Description |
|---|---|---|---|
-KeyName |
string |
Yes | Name used to identify the secret |
-Value |
string |
No | Secret value to store. Omit to retrieve. |
-IfExists |
string |
No | Override or Skip. Controls behavior when the key already exists. Omitting triggers an interactive prompt (default: Skip). |
-Provider |
string |
No | Storage backend: Keyring (default), Environment, or File. |
-FilePath |
string |
No | Path to the secrets file. Used with -Provider File. Prompted if omitted. |
-Set |
switch | No | Store mode. Prompts for the secret with masked input if -Value is omitted. Key existence is checked before the value prompt. |
-Get |
switch | No | Retrieve mode. Equivalent to omitting all mode flags. |
-Remove |
switch | No | Remove the secret. Prompts [y/N] for confirmation (default: No). |
-Force |
switch | No | Skip the confirmation prompt. Only valid with -Remove. |
| Code | Meaning |
|---|---|
0 |
Success |
1 |
Error (key not found, write failed, invalid parameter combination, etc.) |
MIT License
Copyright (c) 2026 zsu
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.