Tuesday, 16 Jul 2024

Automatically Encrypt File and Folders Using PowerShell

powershell encrypt file

In today’s dangerous cyber environment, it’s more important than ever to protect your data. As a sysadmin, securing your network perimeter is crucial, but what if your network is breached? Encrypting your data beforehand can ensure that even if it’s stolen, it won’t be readable.

Encrypting data across different servers and storage locations can be challenging, but using Microsoft’s built-in Encrypting File System (EFS) technology and PowerShell, it becomes much easier. In this article, we’ll explore how to manually encrypt and decrypt files with EFS using the GUI, followed by automating the process using PowerShell.

Encrypt Files via the GUI

To encrypt a file using the GUI, simply right-click on the file in Windows Explorer and select Properties. In the Properties pane, click on the Advanced button. From there, you can check the “Encrypt contents to secure data” checkbox to encrypt the file instantly.

Shows how to encrypt the file using the GUI.

Automating File Encryption

In a business environment, manually encrypting multiple folders across different locations can be time-consuming. That’s where PowerShell comes in handy. By using a PowerShell script, you can automate the encryption process for any number of files or folders, regardless of their location.

Tham Khảo Thêm:  Starlink Business Adds Two New Tiers Of Service

Microsoft has made it easy to encrypt and decrypt files using PowerShell. Simply call the Encrypt() and Decrypt() methods on a specific object obtained through Get-Item (for a single file) or Get-ChildItem (for multiple files or folders).

For example, to encrypt a file with PowerShell, you can use the following code:

(Get-Item -Path C:Groups.csv).Encrypt()

To decrypt the same file:

(Get-Item -Path C:Groups.csv).Decrypt()

To encrypt an entire folder, use Get-ChildItem to retrieve all the files within that folder. Here’s an example:

(Get-ChildItem -Path C:Documents).Encrypt()

Using PowerShell Functions to Encrypt Files

If you prefer using PowerShell functions and cmdlets instead of .NET methods like Encrypt() and Decrypt(), you can create “wrapper” functions for a more intuitive experience. Here’s an example of how this can be done:

# Download the example script to test this out
# Open a PowerShell console and "dot source" the script into your current session
. C:EFS.ps1

# Now you can use the functions to encrypt and decrypt files
# For example, to encrypt a file, use Enable-FileEncryption
Get-Item C:Groups.csv | Enable-FileEncryption

# To decrypt, use Disable-FileEncryption
Get-Item C:Groups.csv | Disable-FileEncryption

# To encrypt a folder, use Get-ChildItem to enumerate all files in the folder
Get-ChildItem C:Documents | Enable-FileEncryption

# Encrypt multiple folders by adding them to Get-ChildItem
Get-ChildItem C:Documents,C:Documents2 | Enable-FileEncryption

This approach is more intuitive and easier to understand. The next time you need to encrypt files or folders, remember that PowerShell can help you accomplish this task efficiently.

Frequently Asked Questions

  • Can I encrypt files and folders across different servers?

    • Yes, you can encrypt files and folders across different servers using PowerShell.
  • Is encrypting data a good security practice?

    • Yes, encrypting data adds an extra layer of security, especially in the event of a breach or physical theft.
  • What are the benefits of using PowerShell for file encryption?

    • Using PowerShell allows for automation and can simplify the process of encrypting multiple files and folders.
Tham Khảo Thêm:  Cloning iPhone Made Easy: Your 3 Best Easy-To-Use Solutions


Securing your data is of utmost importance in today’s cyber environment. By leveraging PowerShell and the Encrypting File System (EFS), you can ensure that your files and folders are protected from unauthorized access. Whether you choose to encrypt manually through the GUI or automate the process using PowerShell, taking steps to safeguard your data is a valuable practice.

For more information, visit Eireview.