How to Find and Remove Empty Folders Using PowerShell?

Recently, I got a requirement from a client to remove empty folders from a drive using PowerShell. In this tutorial, I will explain how to find empty folders on your Windows computer using PowerShell.

PowerShell provides different ways to find empty folders on your system. By running a simple command or script, you can quickly identify folders that don’t contain any files or subfolders, allowing you to delete them and reclaim disk space safely.

Find Empty Folders with PowerShell

PowerShell makes it easy to find empty directories using built-in cmdlets and pipelines. Here’s a simple one-liner that lists all empty folders in the current directory:

Get-ChildItem -Directory -Recurse | Where-Object {$_.GetFileSystemInfos().Count -eq 0}

Let’s break down what this command does:

  1. Get-ChildItem -Directory -Recurse: This cmdlet retrieves all the directories in the current location and its subdirectories recursively.
  2. |: The pipeline operator passes the output of the previous command to the next one.
  3. Where-Object {$_.GetFileSystemInfos().Count -eq 0}: This filters the directories based on a condition. It checks if the count of files and subfolders inside each directory is equal to zero, indicating an empty folder.

For example, let’s say you want to find empty folders in the “C:\MyFolder” directory. Open PowerShell and run:

cd C:\MyFolder
Get-ChildItem -Directory -Recurse | Where-Object {$_.GetFileSystemInfos().Count -eq 0}

This will display a list of all empty folders within the “MyFolder” directory and its subdirectories.

The exact output is in the screenshot below; after I executed the above PowerShell script using VS code,

Find Empty Folders with PowerShell

You can also write the PowerShell script like below:

Get-ChildItem -Path "C:\MyFolder" -Directory -Recurse | Where-Object {$_.GetFileSystemInfos().Count -eq 0}

Explanation:

  • The Get-ChildItem cmdlet is used to retrieve the child items (folders) in the specified directory.
  • The -Path parameter specifies the directory path as “C:\MyFolder”.
  • The -Directory switch is used to filter the results to include only directories (folders).
  • The -Recurse switch is used to recursively search for folders in all subdirectories.
  • The Where-Object cmdlet is used to filter the folders based on a condition.
  • The condition {$_.GetFileSystemInfos().Count -eq 0} checks if the count of files and subdirectories within each folder is equal to zero, indicating an empty folder.

This script will output the empty folders found within the “C:\MyFolder” directory and its subdirectories.

Check out How to Use PowerShell to Find Files Modified After a Certain Date?

Remove Empty Folders using PowerShell

Once you have identified the empty folders, you can remove them using the Remove-Item cmdlet. Be cautious when deleting folders, as this action is permanent and cannot be undone.

To delete a specific empty folder using PowerShell, use:

Remove-Item -Path "C:\MyFolder\EmptyFolder" -Force

Replace “C:\MyFolder\EmptyFolder” with the path to the empty folder you want to remove. The -Force parameter ensures that the folder is deleted without prompting for confirmation.

If you want to remove all empty folders within a directory and its subdirectories using PowerShell, you can combine the commands we used earlier:

Get-ChildItem -Directory -Recurse | Where-Object {$_.GetFileSystemInfos().Count -eq 0} | Remove-Item -Force

This command finds all empty folders and then pipes them to the Remove-Item cmdlet for deletion.

Check out How to Create a Log File with Date and Time in PowerShell?

Complete PowerShell Script to Automate Empty Folders

Now, let me show you the complete PowerShell script to automate empty folders.

You can create a PowerShell script to automate the process of finding and removing empty folders. Here is the PowerShell script to scan a specified directory and delete any empty folders it finds.

$path = "C:\MyFolder"

Write-Host "Scanning for empty folders in $path..."

$emptyFolders = Get-ChildItem -Path $path -Recurse -Directory | 
                Where-Object {$_.GetFileSystemInfos().Count -eq 0}

if ($emptyFolders) {
    Write-Host "Found $($emptyFolders.Count) empty folders:"
    $emptyFolders | ForEach-Object {
        Write-Host $_.FullName
        Remove-Item -Path $_.FullName -Force
    }
    Write-Host "Empty folders removed successfully."
} else {
    Write-Host "No empty folders found."
}

This script does the following:

  1. Defines the $path variable with the directory you want to scan (e.g., “C:\MyFolder”).
  2. Displays a message indicating that it’s scanning for empty folders.
  3. Uses Get-ChildItem to find all empty folders recursively and stores them in the $emptyFolders variable.
  4. Checks if any empty folders were found:
  • If empty folders exist, it displays the count and full path of each folder, then removes them using Remove-Item.
  • If no empty folders are found, it displays a message indicating that no empty folders were found.

You can save this script with a .ps1 extension (e.g., RemoveEmptyFolders.ps1) and run it whenever you need to clean up empty folders in a specific directory.

Conclusion

In this tutorial, I explained how to find and remove empty folders using PowerShell. Do let me know if these examples help you.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.