Remove Empty Directories with PowerShell – How to

Removing Empty Directories in Windows 10 is really simple with this small PowerShell script. There is no need to install any program to clean up the empty directories. With the use of PowerShell, which is included in Windows 10, we can easily remove all empty folders and subfolders.

Removing Empty Directories

Below you will find the script to remove empty directories. 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 on, 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.

# Set to true to test the script
$whatIf = $true

# Remove hidden files, like thumbs.db
$removeHiddenFiles = $false

# Get hidden files or not. Depending on removeHiddenFiles setting
$getHiddelFiles = !$removeHiddenFiles

# Remove empty directories locally
Function Delete-EmptyFolder($path)
{
    # Go through each subfolder, 
    Foreach ($subFolder in Get-ChildItem -Force -Literal $path -Directory) 
    {
        # Call the function recursively
        Delete-EmptyFolder -path $subFolder.FullName
    }

    # Get all child items
    $subItems = Get-ChildItem -Force:$getHiddelFiles -LiteralPath $path

    # If there are no items, then we can delete the folder
	# Exluce folder: If (($subItems -eq $null) -and (-Not($path.contains("DfsrPrivate")))) 
    If ($subItems -eq $null) 
    {
        Write-Host "Removing empty folder '${path}'"
        #Remove-Item -Force -Recurse:$removeHiddenFiles -LiteralPath $Path -WhatIf:$whatIf
    }
}

# Run the script
Delete-EmptyFolder -path "C:\enter\your\path\here"

So 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. It there aren’t any child items, then we the script will remove the empty directories.

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.

removing empty directories in Windows 10
  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.
Remove Empty Directories

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.

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

Get more stuff like this

IT, Office365, Smart Home, PowerShell and Blogging Tips

I hate spam to, so you can unsubscribe at any time.

Leave a Comment

0 Shares
Tweet
Pin
Share
Share