How To Find Empty Folders Using Powershell

Anytime you’re doing cleanup on a share or a local directory, it’s always a good idea to prep and find empty folders using Powershell. A good use case is if you’re going to permanently archive a folder, and you want to do some cleanup to remove the excess baggage of folders that no longer have anything inside them, this would be an excellent tool to make that happen.

This Powershell script uses Get-ChildItem under the hood and also supports the Depth, Recurse and Path Parameters to give you a bit more flexibility on your queries. It also allows you to use UNC paths as well so you can easily use this for network drives if that’s what you choose to audit.

How To Find Empty Folders Using Powershell

Let’s move on to the script on how to find empty folders using Powershell.

Function Get-EmptyDirectory {
    Get empty directories using underlying Get-ChildItem cmdlet

    Name: Get-EmptyDirectory
    Author: theSysadminChannel
    Version: 1.0
    DateCreated: 2021-Oct-2
     https://thesysadminchannel.com/find-empty-folders-powershell/ -
    Get-EmptyDirectory -Path \\Server\Share\Folder -Depth 2 


            Mandatory = $true,
            Position = 0
        [string]    $Path,

            Mandatory = $false,
            Position = 1
        [switch]    $Recurse,

            Mandatory = $false,
            Position = 2
        [int]    $Depth

    BEGIN {}

        try {
            $ItemParams = @{
                Path      = $Path
                Directory = $true
            if ($PSBoundParameters.ContainsKey('Recurse')) {

            if ($PSBoundParameters.ContainsKey('Depth')) {
            $FolderList = Get-ChildItem @ItemParams | select -ExpandProperty FullName

            foreach ($Folder in $FolderList) {
                if (-not (Get-ChildItem -Path $Folder)) {
                        EmtpyDirectory = $true
                        Path           = $Folder
                } else {
                        EmtpyDirectory = $false
                        Path           = $Folder
        } catch {
            Write-Error $_.Exception.Message

    END {}

Get Empty Directory Powershell Script

The Script is showing EmptyFolder as True. Windows Explorer also confirms this.


If you’re looking for a quick and easy way to find empty folders using Powershell, this script will definitely do the job and do it well. The great thing about it is that it is extremely portable so you can copy and paste this script onto another machine and run it locally if you prefer not to have it ran remotely.

For more content like this, be sure to check out our real world Powershell scripts just like this to make sure your sysadmin journey a little easier.

5/5 - (15 votes)

Paul Contreras

Hi, my name is Paul and I am a Sysadmin who enjoys working on various technologies from Microsoft, VMWare, Cisco and many others. Join me as I document my trials and tribulations of the daily grind of System Administration.

One Comment

  1. I’d suggest changing the GCI -path parameter to -LiteralPath. Without it, you’ll get false positives if any foldernames have regex characters in them, [] brackets being a good example.

Leave a Reply

Your email address will not be published.