Do Not Put Secret Strings in Plaintext Files ― Calling Windows DPAPI from PowerShell

セキュリティカテゴリを表すパンダのイラスト Security

About This Article
This article was created using an automated generation flow leveraging generative AI. It reviews the .NET ProtectedData specifications and organizes tips on calling the Windows DPAPI from PowerShell to protect secret strings.

Verification Status: 📘 Official API Verified · PowerShell Sample Implemented · Windows PowerShell 5.1 Real Machine Unverified

Do Not Put Secret Strings in Plaintext Files ― Calling Windows DPAPI from PowerShell

You want to avoid writing API keys or passwords directly into configuration files. However, for small automations on a personal PC, setting up a heavy secret management infrastructure is overkill. In such cases, Windows provides a mechanism called DPAPI.

From PowerShell, you can call it using .NET’s System.Security.Cryptography.ProtectedData.

Protecting with the CurrentUser Scope

# CurrentUser specifies protection bound to the "current Windows user".

$scope = [System.Security.Cryptography.DataProtectionScope]::CurrentUser

# Because DPAPI accepts a byte array, convert the string to UTF-8.


# A dummy string for explanation purposes is used here.

$bytes = [Text.Encoding]::UTF8.GetBytes("dummy-secret")

# ProtectedData.Protect() calls the Windows DPAPI.


# The second argument $null specifies that no additional entropy is used.

$protected = [System.Security.Cryptography.ProtectedData]::Protect(
    $bytes,
    $null,
    $scope
)

# After encryption, it is a byte array rather than a string, so save it as binary.

[IO.File]::WriteAllBytes(".secret.bin", $protected)

This example uses a scope bound to the current Windows user. The saved secret.bin is not a plaintext string.

However, This Does Not Make Secret Management “Foolproof”

CurrentUser can be decrypted within the context of the same Windows user. In other words, it is not an all-purpose mechanism that can protect against scenarios where the device or user account itself is compromised.

Additionally, if you only back up the encrypted file, you may lose the ability to decrypt it if you lose the user profile or the information required by DPAPI.

It is easiest to start using this for purposes such as:

  • Auxiliary scripts on a personal PC

  • Small-scale automation where you want to avoid plaintext storage

  • Educational material for understanding Windows secret protection mechanisms

and so on.

Do Not Write Secrets to the Command Line

Passing them as arguments like -Secret "本物のパスワード" may cause them to remain in history and logs. In the GitHub sample, input is handled via Read-Host -AsSecureString, and conversion memory is cleared as much as possible.

flowchart LR
    A[秘密文字列] --> B[ProtectedData.Protect]
    B --> C[暗号化バイト列]
    C --> D[ファイル保存]
    D --> E[同じユーザーでUnprotect]

GitHub Sample

Official Information & Primary Sources

Document information

Article title
Do Not Put Secret Strings in Plaintext Files ― Calling Windows DPAPI from PowerShell
Published
Updated
Source
https://papanda925.com/?p=15233&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL