cipher - Encrypt Folders and Wipe Free Space
Table of Contents
The cipher command manages encryption (EFS) of files and folders on NTFS drives. It can also overwrite the traces of deleted files in a drive’s free space.
This article focuses on two uses:
- Encrypting and decrypting folders and files (
/e,/d) - Wiping deleted data left in free space (
/w)
Basic Usage
cipher [/e | /d] [/s:folder] [/a] [/i] [/h] [path ...]
cipher /w:folder
cipher /x[:encrypted_file] output_file_name
| Option | Description |
|---|---|
/e | Encrypts the specified folder. Files added to it later are also encrypted |
/d | Decrypts the specified folder |
/s:folder | Applies the operation to the specified folder and all its subfolders |
/a | Applies the operation to files as well as folders |
/i | Continues even if errors occur |
/h | Includes files with the hidden or system attribute |
/w:folder | Overwrites data left in the unused space of the volume |
/x | Backs up the certificate and private key used for encryption to a file |
Checking the Encryption Status
Run without arguments, it lists the encryption status of the files and folders in the current folder.
The letter at the start of each line shows the status.
| Letter | Meaning |
|---|---|
U | Not encrypted (Unencrypted) |
E | Encrypted |
The output above is an example. The wording of the messages varies by Windows version and display language.
What to Know Before Encrypting
Encryption with cipher uses EFS (Encrypting File System). Check the following before you use it.
- It works only on NTFS drives. FAT32 and exFAT drives (such as USB flash drives) can’t be encrypted
- EFS isn’t available in Windows Home editions. You need Pro, Enterprise, Education, or a similar edition
- Encrypted files can be opened only by users who hold the certificate and private key used to encrypt them. Other users and other operating systems can’t read them
- If you copy an encrypted file to a non-NTFS drive such as FAT32 or exFAT, the copy is an unencrypted (plain) file
- EFS doesn’t work on ReFS drives either
EFS keys are tied to your user account. If you lose the key, for example by reinstalling Windows or deleting the account, you can never open the encrypted files again. The certificate is created automatically the first time you encrypt something, so back it up right away as described in “Backing Up Your Key” below.
Encrypting a Folder
Run /e with a folder to make it an encrypted folder.
cipher /e "C:\work\secret"
In this state, files you add later are encrypted automatically. To also encrypt the files already in the folder, add /s and /a.
cipher /e /a /s:"C:\work\secret"
In File Explorer, the names of encrypted files are shown in green by default.
If the parent folder isn’t encrypted, an encrypted file may be saved in decrypted form when you edit it. Encrypt the folder that contains the files, not only the files themselves.
Enclose paths with spaces, such as C:\My Documents, in ". With /s:, enclose the part after it, as in /s:"C:\My Documents".
Decrypting
To decrypt, use /d instead of /e.
cipher /d /a /s:"C:\work\secret"
Backing Up Your Key
This exports the certificate and private key used for encryption as a .pfx file. You’ll be asked for a password, and the file is protected with it. If you haven’t encrypted anything yet, encrypt a folder first so the certificate is created.
cipher /x "%USERPROFILE%\Desktop\efs-backup"
This creates efs-backup.pfx on your desktop. Store it in a different place from the encrypted files, such as an external drive. If you keep both on the same drive, a failure of that drive loses both.
A .pfx file contains your private key. Anyone who knows the password can open your encrypted files, so be careful with where you store it and with the password.
Wiping Deleted Data (/w)
Deleting a file doesn’t immediately erase its data from the disk. The space is just marked as free, and data recovery software may be able to restore it.
cipher /w overwrites the unused space of a volume so that deleted data is harder to recover. Use it, for example, before handing over or disposing of a PC.
cipher /w:C:\
For /w:, specify any folder on the volume you want to wipe. The unused space of the whole volume containing that folder is processed. /w ignores other options, so combining it with /e or similar has no effect.
It overwrites the free space three times, with 0x00, 0xFF, and random data, in that order. Depending on the drive size and free space, it can take tens of minutes to several hours.
/w wipes only space that has already been deleted and is unused. Your current files are left as they are. However, the drive’s free space is temporarily filled while it runs, so avoid other work in the meantime.
Data of very small files (under about 1 KB) is stored in NTFS’s management area (the MFT), so /w may not overwrite it.
On SSDs, wear leveling spreads writes around, so some areas that still hold the original data may remain even after overwriting. To reliably erase SSD data, encrypt the whole drive with BitLocker or similar from the start, or use the manufacturer’s erase tool (such as Secure Erase).
Using It in a Batch File
This batch file checks that the folder exists before encrypting, then shows the result.
@echo off
set "TARGET=%~1"
if "%TARGET%"=="" (
echo Usage: encrypt-folder.cmd folder_path
exit /b 1
)
if not exist "%TARGET%\" (
echo Folder not found: "%TARGET%"
exit /b 1
)
cipher /e /a /s:"%TARGET%"
echo.
echo Check the result. Entries marked E are encrypted.
cipher "%TARGET%"
For arguments and existence checks, see
and .The exit code of cipher isn’t officially defined. Even when it fails, for example on a non-NTFS drive, the exit code may not tell you, so check the output and the encryption status (E) as in the example above.
Summary
| Goal | Command |
|---|---|
| Check the encryption status | cipher |
| Encrypt a folder | cipher /e "folder" |
| Encrypt including existing files | cipher /e /a /s:"folder" |
| Decrypt | cipher /d /a /s:"folder" |
| Back up the certificate and key | cipher /x "output_file_name" |
| Wipe deleted data | cipher /w:C:\ |