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:
Get-ChildItem -Directory -Recurse: This cmdlet retrieves all the directories in the current location and its subdirectories recursively.|: The pipeline operator passes the output of the previous command to the next one.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,

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-ChildItemcmdlet is used to retrieve the child items (folders) in the specified directory. - The
-Pathparameter specifies the directory path as “C:\MyFolder”. - The
-Directoryswitch is used to filter the results to include only directories (folders). - The
-Recurseswitch is used to recursively search for folders in all subdirectories. - The
Where-Objectcmdlet 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" -ForceReplace “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 -ForceThis 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:
- Defines the
$pathvariable with the directory you want to scan (e.g., “C:\MyFolder”). - Displays a message indicating that it’s scanning for empty folders.
- Uses
Get-ChildItemto find all empty folders recursively and stores them in the$emptyFoldersvariable. - 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:
- How to Use PowerShell new-item to Create Files and Directories?
- How to Sort Files by Date in PowerShell?
- How to Get Unique Lines from a File Using PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.