How to Check if a Folder is Empty in PowerShell?

While working with files and directories, you might encounter the need to check if a folder is empty. PowerShell provides different ways to do this. In this PowerShell tutorial, we will explore different methods for checking for an empty directory using PowerShell.

To check if a folder is empty in PowerShell, you can use the Get-ChildItem cmdlet and verify the contents count. For example:

$folderItems = Get-ChildItem -Path "C:\MyFolder"
$isFolderEmpty = $folderItems.Count -eq 0

If $isFolderEmpty is $true, the folder is empty; otherwise, it contains files or subdirectories.

Check if a Folder is Empty in PowerShell

I will show you here 5 methods to check if a folder is empty using PowerShell. All the scripts I have executed and tested.

Method 1: Using the Get-ChildItem Cmdlet

The Get-ChildItem cmdlet is used to list the contents of a directory. By checking the count of items within the folder, we can determine if it’s empty.

Here is the complete PowerShell script.

$directoryPath = "C:\MyFolder"
$items = Get-ChildItem -Path $directoryPath
if ($items.Count -eq 0) {
    Write-Host "'$directoryPath' directory is empty"
} else {
    Write-Host "'$directoryPath' directory is not empty"
}

This script sets the path of the directory you want to check and then uses Get-ChildItem to retrieve the contents. If the count of items is zero, it means the directory is empty.

You can see the output in the screenshot below after I executed the PowerShell script using VS code.

Check if a Folder is Empty in PowerShell

Method 2: Using Get-ChildItem with Additional Checks

Sometimes, you might want to check if a directory is empty, even if it contains hidden files or subdirectories.

The below PowerShell script will check for hidden files also.

$directoryPath = "C:\MyNewFolder"
$items = Get-ChildItem -Path $directoryPath -Recurse -Force
if ($items.Count -eq 0) {
    Write-Host "'$directoryPath' directory is completely empty, including hidden files and subdirectories"
} else {
    Write-Host "'$directoryPath' directory contains files or subdirectories"
}

The -Recurse flag ensures that the command checks all subdirectories, while -Force includes hidden and system files in the count.

Method 3: Using Measure-Object Cmdlet

Another way to check for an empty folder in PowerShell is by using the Measure-Object cmdlet. This cmdlet measures various properties of objects, including the count of items in a directory.

$directoryPath = "C:\MyFolder"
$isEmpty = (Get-ChildItem -Path $directoryPath | Measure-Object).Count -eq 0
if ($isEmpty) {
    Write-Host "'$directoryPath' directory is empty"
} else {
    Write-Host "'$directoryPath' directory is not empty"
}

This script pipes the output of Get-ChildItem to Measure-Object, which counts the items. If the count equals zero, the directory is empty.

Method 4: Using [System.IO.Directory] Class

You can also use the .NET classes within PowerShell, the [System.IO.Directory] class provides a way to interact with file systems.

Below is the complete PowerShell script to check if a directory is empty in PowerShell using the .Net class.

$directoryPath = "C:\MyFolder"
$directoryInfo = [System.IO.Directory]::GetFileSystemEntries($directoryPath)
if ($directoryInfo.Length -eq 0) {
    Write-Host "'$directoryPath' directory is empty"
} else {
    Write-Host "'$directoryPath' directory is not empty"
}

This script uses the GetFileSystemEntries method to retrieve the entries of the specified directory. If the length of the array is zero, the directory is empty.

Conclusion

Checking if a directory is empty is a very common requirement in PowerShell. In this PowerShell, I have explained different methods to check if a folder is empty in PowerShell like by using the Get-ChildItem Cmdlet and by using the Measure-Object Cmdlet, etc.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.