How to Find Empty Folders in PowerShell (With Example)


You can use the following basic syntax in PowerShell to find all empty folders in a specific directory:

dir -Directory | ? {$_.GetFileSystemInfos().Count -eq 0 }

This particular example will return the name of each folder that is empty in the current directory.

By using $_GetFileSystemInfos().Count we are able to get the file count for each folder in the current directory.

We then use the -eq operator to filter these folders to only show the ones where the file count is equal to 0.

The following example shows how to use this syntax in practice.

Example: How to Find Empty Folders in PowerShell

Suppose our current directory has the following path:

  • c:\users\bobbi\collecting_wisdom

We can use dir to list all folders in this directory:

We can see that there are five folders in the current directory.

To return all folders that are empty, we can use the following syntax:

dir -Directory | ? {$_.GetFileSystemInfos().Count -eq 0 }

The following screenshot shows how to use this syntax in practice:

PowerShell find empty folders

From the output we can see that two folders in this directory are empty:

  • Backup Data
  • Biostatistics

Each of these folders have zero files in them.

If we’d like, we can use the following syntax to display the total file count for each folder in the current directory:

dir -Directory | % {$_.GetFileSystemInfos().Count}

The following screenshot shows how to use this syntax in practice:

We can see that the folders have the following number of files:

  • Backup Data: 0
  • Biostatistics: 0
  • Econometrics: 7
  • Environmental Stats: 7
  • Time Series Analysis: 9

This confirms that there are indeed two empty folders in this directory.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Check if File is Empty
PowerShell: How to Check if String is Empty
PowerShell: How to Check if File Contains Specific String

Leave a Reply