Skip to main content

Removing empty directories in Windows 10 is really simple with a small PowerShell script or RoboCopy cmd. There is really no need to install any program to clean up the empty directories on your computer or server.

With the use of PowerShell or RoboCopy, both included in Windows 10, we can delete all empty folders and subfolders. It’s always a good idea to list the empty directories first, so I added the option in both scripts.

Below an example for both scripts. I have also tested the PowerShell and RoboCopy script against a directory with 11.000 empty subdirectories to see which option is the fastest.

Remove Empty Directories with PowerShell

Below you will find the script to remove empty directories with PowerShell. If you want to test the script first, which I really recommend doing, then you can set the variable $whatif to $true.

Windows makes a hidden file, Thumbs.db, in folders with images in it. These folders appear to be empty, even with show hidden files in Explorer, you don’t see the Thumbs.db. So to remove those empty folders as well you can set removeHiddenFiles to $true.

The script will then ignore the hidden files and remove the folder including the hidden files with it.

1.  # Set to true to test the script
2.  $whatIf = $true
3.
4.  # Remove hidden files, like thumbs.db
5.  $removeHiddenFiles = $false
6.
7.  # Get hidden files or not. Depending on removeHiddenFiles setting
8.  $getHiddelFiles = !$removeHiddenFiles
9.
10. # Remove empty directories locally
11. Function Delete-EmptyFolder($path)
12. {
13.     # Go through each subfolder, 
14.     Foreach ($subFolder in Get-ChildItem -Force -Literal $path -Directory) 
15.     {
16.         # Call the function recursively
17.         Delete-EmptyFolder -path $subFolder.FullName
18.     }
19.
20.     # Get all child items
21.     $subItems = Get-ChildItem -Force:$getHiddelFiles -LiteralPath $path
22.
23.     # If there are no items, then we can delete the folder
24.     # Exluce folder: If (($subItems -eq $null) -and (-Not($path.contains("DfsrPrivate")))) 
25.     If ($subItems -eq $null) 
26.     {
27.         Write-Host "Removing empty folder '${path}'"
28.         Remove-Item -Force -Recurse:$removeHiddenFiles -LiteralPath $Path -WhatIf:$whatIf
29.     }
30. }
31.
32. # Run the script
33. Delete-EmptyFolder -path "C:\enter\your\path\here"

In the script, we first set the WhatIf mode. This way we can test the script without actually removing any folders.

Next, we have the function Delete-EmptyFolder. The function will go through each subfolder of the given path and calls the function recursively to process the subfolders as well.

If there aren’t any subfolders, then it will request all child items in the current folder. If there aren’t any child items, then the script will remove the empty directories.

How fast is PowerShell with removing empty directories?

I tested both scripts against a directory with 11.000 empty subdirectories, going 3 levels deep. PowerShell needed 12 secs to remove all empty directories. That is when you disable the Write-Host output on line 27. With the output, it took 24 secs in total.

Running the script

The easiest way to run this script is to open the Windows PowerShell ISE. This way you can easily change the path and WhatIf variable. Windows PowerShell ISE is also a standard program of Windows 10 which you will find in your Start Menu.

  1. Click on Start
  2. Type PowerShell
  3. Select Windows PowerShell ISE
  4. Copy the script above
  5. Paste the code in the script section (you may need to expand it first, see red arrow)
  6. Change the path and make sure WhatIf is set to true first
  7. Click the green button to run the script.

Delete empty folders with RoboCopy

Another option to delete empty folders is to use RoboCopy. This command-line utility is built into Windows since Windows 7 and can be used in PowerShell or CMD.

The trick with RoboCopy is actually to move the directory to the same folder. The /S option copies all subdirectories but excludes the empty folders. Just make sure that you use the exact same path twice in RoboCopy

1.  # Make sure both paths are exactly the same
2.  robocopy "c:\path\to\directory" "c:\path\to\directory" /S /move

If you first want to list all the empty directories with RoboCopy you can use the /L switch.

1.  robocopy "c:\path\to\directory" "c:\path\to\directory" /S /move /L

Downside of using RoboCopy

There is one downside to using the RoboCopy method, it won’t remove empty directories with a Thumbs.db in it. This is a little file that is created by Windows in folders with images in it. You can safely remove this file if the directory is further empty.

With PowerShell we can ignore the file and remove it together with the empty directory. We can’t do such thing with RoboCopy.

How fast is RoboCopy with deleting empty folders?

Robocopy was remarkable faster than PowerShell. Removing the 11.000 directories only took 4 secs on average.

Wrapping up

Always test script first, use the WhatIf mode, or run it on a couple of test folders so you are sure that it’s working as aspected.

The RoboCopy method is a lot faster then PowerShell, but keep in mind that it won’t remove directories with a Thumbs.db in it.

If you have any questions, just drop a comment below.