How to encrypt a file using PowerShell (and what isn't built in)
You want one PowerShell command that locks a file with a password. Windows doesn't include one. Compress-Archive has no password parameter, and the built-in commands that do encrypt use your Windows account, a certificate, or protect only a stored password. Here is what each option does, with exact commands and a safe folder script.
cipher /e uses EFS, which ties files to your Windows account and isn't available on Windows Home. Protect-CmsMessage encrypts text to a certificate, and Export-Clixml saves a password that only your account on that PC can read. To lock files with a password from PowerShell, call the free 7-Zip or clavis-cli, the command line in Clavis Encrypt Premium.What each option can and can't do
| Option | Windows editions | Locked with | Good for | Can't |
|---|---|---|---|---|
cipher /e (EFS) | Pro, Enterprise, Education | An EFS certificate in your Windows account | Files that stay on this PC's NTFS drives | Not on Home. No password. Copies on non-NTFS drives or in zips come out decrypted. |
Protect-CmsMessage | All, including Home | A document encryption certificate | Notes, tokens, config text; sending to someone's certificate | Built for text: it decrypts to a string, not a photo or PDF. |
Export-Clixml, ConvertFrom-SecureString | All (encrypted on Windows only) | DPAPI: your account on this PC | Storing a password for a script | Doesn't encrypt your files. Other users and PCs can't read it. |
7-Zip (7z.exe) | All (free download) | A password | Files and folders you keep or send | No password recovery. A password typed into the command is visible. |
clavis-cli | Windows 10 and 11 (64-bit), Linux; Premium | Your Clavis vault password | Scripts and scheduled jobs (--password-stdin) | Not in the Microsoft Store version. Doesn't encrypt whole drives. |
None of these encrypt a whole drive. For a lost or stolen laptop, BitLocker or device encryption is a separate layer.
1. cipher /e: EFS (Pro, Enterprise and Education)
cipher is a Windows command you can run in PowerShell. /e encrypts the files or folders you name; a folder is marked so that files added to it later are encrypted too. /s: does the same for every subfolder, and /x backs up your EFS certificate and keys to a file.
cipher /e /s:"$env:USERPROFILE\Documents\Private"
cipher /x "$env:USERPROFILE\Desktop\efs-key-backup"Microsoft says file encryption isn't available in the Home edition. On a Windows 11 Home PC, cipher /e answered "The request is not supported." when we tried it. EFS has no password of its own: Microsoft describes it as transparent to the user who encrypted the file, so anything running as you opens it. Files can become decrypted when copied to a drive that isn't NTFS, and EFS files added to a zipped folder are unencrypted when unzipped. More in why "Encrypt contents to secure data" is greyed out.
2. Protect-CmsMessage: encrypt with a certificate (all editions)
This is the built-in answer to "PowerShell encrypt file with certificate". It uses the Cryptographic Message Syntax standard: anyone with your certificate's public key can encrypt, and only the matching private key decrypts. You need a certificate marked for document encryption first.
New-SelfSignedCertificate -Subject "CN=you@example.com" -Type DocumentEncryptionCert -CertStoreLocation Cert:\CurrentUser\My
Protect-CmsMessage -To "*you@example.com*" -Path .\notes.txt -OutFile .\notes.txt.cms
Unprotect-CmsMessage -Path .\notes.txt.cmsThis worked in our test on Windows 11 Home (Windows PowerShell 5.1). Microsoft's page shows another way to make one, with certreq. notes.txt stays as it was; delete it only after Unprotect-CmsMessage gives the text back. Two limits: Unprotect-CmsMessage returns a string, so this suits text, not photos, PDFs or Office files. And the private key lives in your certificate store: without a backup of it, a new PC can't decrypt. To send to someone, encrypt with their certificate.
3. Encrypt a password to a file: Export-Clixml
For "PowerShell encrypt password to file", this is the tool. It protects a stored password, not your documents.
Get-Credential -UserName backup -Message "Password to store" | Export-Clixml "$env:LOCALAPPDATA\backup.cred"
$cred = Import-Clixml "$env:LOCALAPPDATA\backup.cred"Microsoft says Export-Clixml encrypts credentials with the Windows Data Protection API, so "only your user account on only that computer" can decrypt them. ConvertFrom-SecureString does the same for one secure string when you give it no key. Windows PowerShell 5.1 shows a sign-in dialog for Get-Credential; PowerShell 7 asks in the console. On Linux and macOS, Export-Clixml stores the password as plain text.
4. 7-Zip from PowerShell (free, any edition)
$7z = "C:\Program Files\7-Zip\7z.exe"
& $7z a -p -mhe=on "D:\Encrypted\Reports.7z" "$env:USERPROFILE\Documents\Reports"
& $7z t "D:\Encrypted\Reports.7z"-p with nothing after it makes 7-Zip ask for the password, so it never lands in your history. -mhe=on encrypts the file names too (.7z only). The & is PowerShell's call operator, which runs a command stored in a variable. The .7z uses AES-256. Details, quoting and extracting: 7-Zip command line password guide.
5. clavis-cli from PowerShell (Clavis Encrypt Premium)
clavis-cli is the command line of Clavis Encrypt, part of Premium ($29 once, tax included). It comes with the download from clavisenc.com, whose Windows installer adds it to PATH; the Microsoft Store version has no command line. It uses your Clavis vault password, the same AES-256-GCM encryption as the app, and gives each file a random name with the real one stored inside. Clavis is closed source and hasn't been independently audited; its file format is public, with an open-source decryptor. Open a new PowerShell window after installing; if clavis-cli isn't recognized, run the latest installer from the download page once.
clavis-cli encrypt --keep "$env:USERPROFILE\Documents\Reports"
clavis-cli verify "$env:USERPROFILE\Documents\Reports"
clavis-cli list "$env:USERPROFILE\Documents\Reports"It asks for the password without showing it and never accepts one as an argument. Without --keep, it deletes the originals after encrypting (best effort on SSDs). Exit codes: 0 ok, 1 some files failed, 2 usage error, 3 needs Premium, 4 wrong password or key file. All options: clavis-cli help.
A short, safe script: encrypt the files in a folder
It uses 7-Zip, so it works for anyone. It stops on any problem, puts everything in one dated .7z, tests it, and leaves your originals alone.
$ErrorActionPreference = 'Stop'
$source = "$env:USERPROFILE\Documents\Reports"
$target = "D:\Encrypted"
$7z = "C:\Program Files\7-Zip\7z.exe" # 32-bit 7-Zip: C:\Program Files (x86)\7-Zip\7z.exe
if (-not (Test-Path -LiteralPath $7z)) { throw "7-Zip not found: $7z" }
if (-not (Test-Path -LiteralPath $source)) { throw "Folder not found: $source" }
New-Item -ItemType Directory -Force -Path $target | Out-Null
$archive = Join-Path $target ("Reports-" + (Get-Date -Format "yyyy-MM-dd") + ".7z")
if (Test-Path -LiteralPath $archive) { throw "Already exists: $archive" }
& $7z a -p -mhe=on $archive $source # asks for the password
if ($LASTEXITCODE -ne 0) { throw "7-Zip stopped with exit code $LASTEXITCODE" }
& $7z t $archive # asks again: proves the password works
if ($LASTEXITCODE -ne 0) { throw "Test failed. Keep your originals." }
"Done: $archive. Your originals are untouched."7-Zip documents only two ways to take a password, in -p or typed at the prompt, so an unattended 7-Zip job would need it in the command. clavis-cli reads it from standard input instead. Here it comes from a file saved with Export-Clixml as in step 3, named clavis.cred:
$ErrorActionPreference = 'Stop'
Get-Command clavis-cli | Out-Null # stops here if clavis-cli isn't installed
$OutputEncoding = [System.Text.UTF8Encoding]::new() # Windows PowerShell 5.1 pipes ASCII by default
$pw = (Import-Clixml "$env:LOCALAPPDATA\clavis.cred").GetNetworkCredential().Password
$out = Join-Path "D:\Encrypted" (Get-Date -Format "yyyy-MM-dd_HHmm")
New-Item -ItemType Directory -Force -Path $out | Out-Null
$pw | clavis-cli encrypt --keep --out $out --password-stdin "$env:USERPROFILE\Documents\Exports"
if ($LASTEXITCODE -ne 0) { throw "clavis-cli stopped with exit code $LASTEXITCODE" }With --out, every .enc file lands straight in that folder (subfolders aren't recreated), and each run makes a new copy of every file, hence a new folder per run. Run the scheduled task as the same Windows account that saved clavis.cred: DPAPI lets only that account unlock it. For a nightly pattern with a staging folder and a verify step, see encrypting files from the command line.
What can go wrong
- A lost password or key means lost files. 7-Zip has no password reset. EFS files need your EFS key, so keep the
cipher /xbackup off the PC. CMS files need the certificate's private key. Clavis Encrypt gives you a recovery key; if you lose both it and the password, nobody can recover the files. - Passwords in scripts and history. Microsoft warns that plain-text passwords typed in commands "can show up in event logs and command history logs". PSReadLine saves your commands to
%APPDATA%\Microsoft\Windows\PowerShell\PSReadLine; it skips lines containing words like password or secret, but7z a -pBlue42 …contains neither. While a program runs, other programs can read its command line. Use the prompt, a DPAPI-protected file or--password-stdin. - Accented letters turn into "?". Windows PowerShell 5.1 pipes text to programs as ASCII unless you set
$OutputEncoding, as in the script above. In our test, "ä" arrived as "?".
Questions
Can PowerShell encrypt a file with a password?
Not with a built-in cmdlet. Compress-Archive has no password parameter, cipher /e uses your Windows account rather than a password, and Protect-CmsMessage uses a certificate. To add a password, call 7-Zip (free) or clavis-cli (Clavis Encrypt Premium) from PowerShell.
How do I encrypt a file with a certificate in PowerShell?
Create a document encryption certificate (for example New-SelfSignedCertificate -Type DocumentEncryptionCert), then run Protect-CmsMessage -To with the certificate's subject, -Path and -OutFile. Unprotect-CmsMessage decrypts it. It returns text, so it suits text files rather than photos or Office documents.
How do I save an encrypted password to a file in PowerShell?
Pipe Get-Credential to Export-Clixml, and load it later with Import-Clixml. On Windows it's encrypted with DPAPI, so only your user account on that computer can read it. On Linux and macOS it's stored as plain text.
Does cipher /e work on Windows 11 Home?
No. Microsoft says file encryption isn't available in the Home edition, and on a Windows 11 Home PC cipher /e answered "The request is not supported." when we tried it. Use 7-Zip or a file-encryption app instead.
Can I decrypt an Export-Clixml password on another computer?
No. Microsoft says the exported file can't be used on a different computer or by a different user. Save the password again on each PC that needs it.
How do I encrypt every file in a folder with PowerShell?
Run 7-Zip with 7z a -p -mhe=on archive.7z and the folder, check $LASTEXITCODE, then test the archive with 7z t before you delete anything. With Clavis Encrypt Premium, clavis-cli encrypt --keep and the folder encrypts each file separately.
Is ConvertTo-SecureString encryption?
ConvertTo-SecureString only makes a secure string. ConvertFrom-SecureString turns it into an encrypted string you can save, with DPAPI or, if you pass a key, AES. On Linux and macOS, SecureString contents aren't encrypted.
Sources
- Microsoft Learn: Compress-Archive (parameters; no password option)
- Microsoft Support: How to encrypt a file or folder (not available in the Home edition)
- Microsoft Learn: cipher (/e, /s, /x)
- Microsoft Learn (archived): Encrypting File System overview (transparent to the user; copies to non-NTFS volumes can become decrypted)
- Microsoft Support: Zip and unzip files (encrypted files are unencrypted when unzipped)
- Microsoft Learn: Protect-CmsMessage (CMS, certificate requirements, certreq example)
- Microsoft Learn: Unprotect-CmsMessage (returns a string)
- Microsoft Learn: New-SelfSignedCertificate (-Type DocumentEncryptionCert)
- Microsoft Learn: Export-Clixml (DPAPI; only your account on that computer; plain text on Linux and macOS)
- Microsoft Learn: ConvertFrom-SecureString (DPAPI without a key, AES with one)
- Microsoft Learn: ConvertTo-SecureString (plain text can show up in event logs and history)
- Microsoft Learn: Get-Credential (dialog in Windows PowerShell 5.1, console in 6.0 and later)
- Microsoft Learn: PSCredential.GetNetworkCredential
- Microsoft Learn: about_Operators (call operator &)
- Microsoft Learn: about_Automatic_Variables ($LASTEXITCODE)
- Microsoft Learn: about_Preference_Variables, Windows PowerShell 5.1 ($OutputEncoding defaults to ASCII)
- Microsoft Learn: about_PSReadLine (history file and its sensitive-word filter)
- Microsoft Learn: Win32_Process (CommandLine property)
- 7-Zip: 7z format (AES-256 encryption)
- 7-Zip source: 7zipInstall.c (the installer's default folder: Program Files\7-Zip, from Windows' ProgramFilesDir setting)
- 7-Zip help: -p (set Password) switch, -mhe encrypts file names (online copy of the help that ships with 7-Zip; the manual in the current download adds that -p without a password makes 7-Zip ask for one)
- 7-Zip help: exit codes
Last checked against Microsoft's and 7-Zip's documentation on November 11, 2026. The cipher, Protect-CmsMessage and $OutputEncoding results are from our own tests on a Windows 11 Home PC with Windows PowerShell 5.1, November 2026.
Related: password-protecting a zip file in Windows 11, encrypting a file with OpenSSL on Windows and encrypting a file with GPG on Windows.
One-time payment, tax included, no subscription. The command line comes with the installer from clavisenc.com, not the Microsoft Store version, so start with the free installer.