0

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 {
<#
.SYNOPSIS
    Get empty directories using underlying Get-ChildItem cmdlet

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

    [CmdletBinding()]

    param(
        [Parameter(
            Mandatory = $true,
            Position = 0
        )]
        [string]    $Path,

        [Parameter(
            Mandatory = $false,
            Position = 1
        )]
        [switch]    $Recurse,

        [Parameter(
            Mandatory = $false,
            Position = 2
        )]
        [ValidateRange(1,15)]
        [int]    $Depth
    )

    BEGIN {}

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

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

            foreach ($Folder in $FolderList) {
                if (-not (Get-ChildItem -Path $Folder)) {
                    [PSCustomObject]@{
                        EmtpyDirectory = $true
                        Path           = $Folder
                    }
                } else {
                    [PSCustomObject]@{
                        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.

Conclusion

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 - (14 votes)

Paul

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.