Delete Empty Folders in PowerShell

Migel Hewage Nimesha Jun 02, 2022
  1. Get-ChildItem Cmdlet in PowerShell
  2. Where-Object Cmdlet in PowerShell
  3. Delete Items From a Collection in PowerShell
Delete Empty Folders in PowerShell

This article shows ways to delete empty folders in PowerShell.

Get-ChildItem Cmdlet in PowerShell

The Get-ChildItem cmdlet retrieves all the child items (files and folders) for a specified location. When there is a need to delete all the empty folders in a given folder path, it is a must to traverse the folder hierarchy.

Hence, the Get-ChildItem cmdlet would be helpful. This cmdlet accepts parameters like Recurse and Directory to fetch directory-type child items and recursively traverse the folder structure.

Syntax:

Get-ChildItem -Path -Directory -Recurse

There are many more optional parameters available to use. Let’s create a folder structure as shown in the following.

somepath/testA
			testB
				testD
				testE
					a.txt
			testC
				testF
					b.txt
				testG
					testH

The purpose is to remove all the empty folders within the testA folder. Therefore, the following folders should be deleted.

  1. testD
  2. testG
  3. testH

Let’s fetch all the subdirectories in the given folder path (somepath/testA).

$path = "D:\testA"
$fetchedDirList = Get-ChildItem $path -directory -recurse

Using the alias gci instead of Get-ChildItem is possible.

$fetchedDirList = gci $path -directory -recurse

Let’s check the $fetchedDirList variable.

$fetchedDirList

Output:

Get-ChildItem

All the directories and subdirectories have been fetched as expected.

Where-Object Cmdlet in PowerShell

We need to filter out the empty directories from the above result. The Where-Object cmdlet filters object from a collection based on their properties.

The Where alias can be used instead of the Where-Object command. We need to filter the above directory list based on the item count within each directory.

The condition is shown in the following.

Where { (gci $_.fullName).count -eq 0 }

When the given object from the collection has 0 sub-items, it is considered an empty directory. Hence, we should delete it.

Let’s pipe the previous step output to the Where cmdlet.

$emptyDirectoryList = $fetchedDirList | Where { (gci $_.fullName).count -eq 0 }

Let’s print the $emptyDirectoryList.

$emptyDirectoryList

Output:

Where-Object

The result is perfectly correct. We got only two folders called testD and testH with empty content.

We can easily delete each object from the $emptyDirectoryList collection. The Remove-Item cmdlet can be used to delete an item.

Before that, we need to fetch the full path of each of the objects inside the $emptyDirectoryList. The Select-Object cmdlet can fetch the objects with its FullName property.

$finalListToRemove = $emptyDirectoryList | select -expandproperty FullName

Output of the $finalListToRemove:

finalListToRemove

Now we got the folders list to be removed.

Delete Items From a Collection in PowerShell

It is possible to use the ForEach-Object cmdlet to loop through the items in a collection. Inside the loop, we can use the Remove-Item cmdlet.

$finalListToRemove | Foreach-Object { Remove-Item $_ }

The $_ denotes the current item in the collection. This will delete the testD and testH folders.

There is a tricky part here. When the testH folder is deleted, the testG directory becomes empty too.

Therefore, we need to modify the script a bit. We should run the above procedure until we are left with non-empty folders.

We can use the do...while loop to do that.

$path = "D:\testA"
do{
$fetchedDirList = Get-ChildItem $path -directory -recurse
$emptyDirectoryList = $fetchedDirList | Where { (gci $_.fullName).count -eq 0 }
$finalListToRemove = $emptyDirectoryList | select -expandproperty FullName
$finalListToRemove | Foreach-Object { Remove-Item $_ }
} while ( $finalListToRemove.count -gt 0 )

Here, we run the while loop until the list of empty folders count becomes 0. When you need to consider the hidden files and folders, we can pass the -Force parameter to the Get-ChildItem cmdlet as shown in the following.

$emptyDirectoryList = $fetchedDirList | Where { (gci $_.fullName -Force).count -eq 0 }
Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.

Related Article - PowerShell Folder