Table of Contents
For this article, we are using three directories, E:\Test\Script Files, E:\Test\Test Files, and E:\Test\Sample Files. The E:\Test\Script Files directory is not empty and contains
test.batandtestPS.ps1files, while the E:\Test\Test Files directory is empty because it contains nothing, neither files nor foldersThe E:\Test\Sample Files directory is also not empty; it contains
file.txt,file1.txt,file2.txtand a folder namedFolderA, which also containsfile.txtfile andFolderBfolder. TheFolderBis empty. Knowing these three directories is mandatory to crosscheck the outputs of the upcoming code examples. Note that using your directories is perfectly fine too. You can still follow this tutorial.
Using Get-ChildItem Cmdlet & Count Property
Use the Get-ChildItem cmdlet and Count property to check if directory is empty in PowerShell.
|
1 2 3 4 5 6 7 8 9 |
$directoryPath = "E:\Test\Script Files" $items = Get-ChildItem -Path $directoryPath if ($items.Count -eq 0) { Write-Host "'$directoryPath' directory is empty." } else { Write-Host "'$directoryPath' directory is not empty." } |
|
1 2 3 |
'E:\Test\Script Files' directory is not empty. |
First, we initialized the $directoryPath variable with the directory we wanted to check whether it was empty. Next, we used the Get-ChildItem cmdlet with the -Path parameter to retrieve all the items and child items from the specified $directoryPaththat we stored in the $item variable for further use.
Then, in the if statement, we used the Count property of $item and checked if it equals 0 using the -eq operator. If it is equal to 0, the Write-Host cmdlet within the if block was executed, stating the directory is empty; otherwise, the Write-Host from the else block will be executed to display the specified directory is not empty.
We can omit the
-Pathparameter and mention$directoryPathwithout it asGet-ChildItem $directoryPath; it will still work as desired.
Let’s practice the same code example below for an empty directory.
|
1 2 3 4 5 6 7 8 9 |
$directoryPath = "E:\Test\Test Files" $items = Get-ChildItem -Path $directoryPath if ($items.Count -eq 0) { Write-Host "'$directoryPath' directory is empty." } else { Write-Host "'$directoryPath' directory is not empty." } |
|
1 2 3 |
'E:\Test\Test Files' directory is empty. |
The Get-ChildItem cmdlet won’t display the hidden files by default, so we must use the -force parameter to ensure the directory is genuinely empty. See the following example.
|
1 2 3 4 5 6 7 8 9 |
$directoryPath = "E:\Test\Test Files" $items = Get-ChildItem $directoryPath -force if ($items.Count -eq 0) { Write-Host "'$directoryPath' directory is empty." } else { Write-Host "'$directoryPath' directory is not empty." } |
|
1 2 3 |
'E:\Test\Test Files' directory is empty. |
According to the above output, the E:\Test\Test Files directory is still empty, so we don’t have any hidden files in the E:\Test\Test Files directory. Note that the -force parameter was used to ensure this.
In the above examples, we are enumerating each file under the specified directory, which is time-consuming. We want to check if the directory is empty, so why go through all files/folders? Finding one file/folder is enough to tell that the directory is not empty. So, how can we do it? See the following example.
|
1 2 3 4 5 6 7 8 9 |
$directoryPath = "E:\Test\Script Files" $items = Get-ChildItem $directoryPath -force | Select-Object -First 1 if ($items.Count -eq 0) { Write-Host "'$directoryPath' directory is empty." } else { Write-Host "'$directoryPath' directory is not empty." } |
|
1 2 3 |
'E:\Test\Script Files' directory is not empty. |
This code example is similar to the previous ones, but we used the Select-Object cmdlet with the -First parameter, which was set to 1. We used the Select-Object cmdlet with the -Firstparameter to select one object from the start of an array of input objects in PowerShell. In simple terms, this code snippet won’t enumerate all the files/folders but stops at the first one found, and it is enough to decide that directory is not empty.
Use Aliases of Get-ChildItem Cmdlet
We have three aliases, dir, ls, and gci, that we can use as an alternative to the Get-ChildItem cmdlet. To check all the available aliases for the Get-ChildItem cmdlet, we can use the Get-Alias -Definition Get-ChildItem command on the PowerShell console. We have three aliases (mentioned above) for Get-ChildItemwhile writing this article. Let’s learn them one by one.
Use gci with the Count property to check if directory is empty in PowerShell.
|
1 2 3 4 5 6 7 8 9 |
$directoryPath = "E:\Test\Test Files" $items = gci -Path $directoryPath if ($items.Count -eq 0) { Write-Host "'$directoryPath' directory is empty." } else { Write-Host "'$directoryPath' directory is not empty." } |
|
1 2 3 |
'E:\Test\Test Files' directory is empty. |
This example is similar to the first example in the previous section, but we used the gci alias of the Get-ChildItem cmdlet this time.
Use the dir alias with the Count property to check if the given directory is empty in PowerShell.
|
1 2 3 4 5 6 7 |
if((dir "E:\Test\Script Files").Count -eq 0) { "Empty" }else{ "Not Empty" } |
|
1 2 3 |
Not Empty |
We used the dir alias to retrieve all the items, including files and directories. Then, we used the Count property to get a count of them and compared it with 0 using the -eq operator. If the value of the Count property is equal to 0, then the given directory is empty; otherwise, not.
Use the ls alias with the Count property to check if the given directory is empty in PowerShell.
|
1 2 3 4 5 6 7 |
if((ls "E:\Test\Script Files").Count -eq 0) { "Empty" }else{ "Not Empty" } |
|
1 2 3 |
Not Empty |
This example is similar to the previous one, but we used ls here, which lists all the files and folders from the specified directory.
In all the above code examples, we will get a message saying the directory is empty/not empty if the specified path is correct; otherwise, we will get an error displaying that the path is not found. To handle this situation, we should devise a solution that only checks the directory if the given path is correct; otherwise, it informs the user by displaying a message instead of an error. So, let’s dive into the solution below.
Using Test-Path & Get-ChildItem Cmdlets
Use the Test-Path and Get-ChildItem cmdlets to check if the given directory is empty in PowerShell.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$directoryPath = "E:\Test\Script Files" if (Test-Path $directoryPath) { $items = Get-ChildItem $directoryPath -force | Select-Object -First 1 if ($items.Count -eq 0) { Write-Host "'$directoryPath' directory is empty." } else { Write-Host "'$directoryPath' directory is not empty." } }else{ Write-Host "The Given Path Doesn't Exist." } |
|
1 2 3 |
'E:\Test\Script Files' directory is not empty. |
Now, specify a random path which does not exist. See the following example to observe the code and its output.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$directoryPath = "E:\Testssss\Script Files" if (Test-Path $directoryPath) { $items = Get-ChildItem $directoryPath -force | Select-Object -First 1 if ($items.Count -eq 0) { Write-Host "'$directoryPath' directory is empty." } else { Write-Host "'$directoryPath' directory is not empty." } }else{ Write-Host "The Given Path Doesn't Exist." } |
|
1 2 3 |
The Given Path Doesn't Exist. |
This time, we got a message stating the path did not exist. It is because we used the Test-Path cmdlet. The Test-Path cmdlet determines if all the elements of the specified path exist. It returns True if all the elements are present; otherwise, it returns False.
We used this cmdlet within the if statement to see if the returned value is True, then jumped into the if block and checked if the $directoryPath is empty. On the contrary, move to the else block and execute the Write-Host cmdlet to display the The Given Path Doesn't Exist. message on the PowerShell console.
The
Test-Pathcmdlet also tells if the specified path syntax is correct. It returnsFalseif the path is a whitespace or an empty string. It returns a non-terminating error if the path is$null, an array of$nullor an empty array. You can find more on that here.
Let’s add more spice to our code snippets and suppose a use case where we must check if the given directory is empty. If empty, print a meaningful message on the PowerShell console. And if it is not empty, count the number of files and directories from the specified directory. Let’s dive into the following section to learn how to do it.
Further reading:
Using Get-ChildItem with the -File Parameter
If the given directory is not empty, then check the files’ count and names in that directory (excluding subdirectories) :
- Use the
Get-ChildItemcmdlet to get all files from the specified path and store them in a variable. - Use
Test-Pathwithin theifstatement to check if the given path exists. Display a message if it doesn’t exist.- If the path exists, use the nested
ifto compare a variable’sCountproperty (variable created in the first step) with0using the-eqoperator. - If it equals
0, print a message saying the given directory is empty. Otherwise, display the given directory is not empty, and display the files’ count and their names.
- If the path exists, use the nested
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$directoryPath = "E:\Test\Sample Files" $files = Get-ChildItem -Path $directoryPath -File if (Test-Path $directoryPath) { if ($files.Count -eq 0) { Write-Host "'$directoryPath' directory is empty." } else { Write-Host "'$directoryPath' directory is not empty. It contains '$($files.Count)' files that are '$files'." } }else{ Write-Host "The Given Path Doesn't Exist." } |
|
1 2 3 |
'E:\Test\Sample Files' directory is not empty. It contains '3' files that are 'file.txt file1.txt file2.txt'. |
We can use the -Recurse parameter to count files from the subdirectories. See the following example.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$directoryPath = "E:\Test\Sample Files" $files = Get-ChildItem -Path $directoryPath -File -Recurse if (Test-Path $directoryPath) { if ($files.Count -eq 0) { Write-Host "'$directoryPath' directory is empty." } else { Write-Host "'$directoryPath' directory is not empty. It contains '$($files.Count)' files that are '$files'." } }else{ Write-Host "The Given Path Doesn't Exist." } |
|
1 2 3 |
'E:\Test\Sample Files' directory is not empty. It contains '4' files that are 'file.txt file1.txt file2.txt file.txt'. |
Similarly, we can do this with directories. See the following section to understand that.
Using Get-ChildItem with the -Directory Parameter
If the given directory is not empty, then check the subdirectories’ count and names in that directory (excluding subdirectories) :
- Use the
Get-ChildItemcmdlet to get all sub-directories from the specified path and store them in a variable. - Use
Test-Pathwithin theifstatement to check if the given path exists. Display a message if it doesn’t exist.- If the path exists, use the nested
ifto compare a variable’sCountproperty (variable created in the first step) with0using the-eqoperator. - If it equals
0, print a message stating the given directory is empty. Otherwise, display the given directory is not empty, and display the count and names of the sub-directories.
- If the path exists, use the nested
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$directoryPath = "E:\Test\Sample Files" $directories = Get-ChildItem -Path $directoryPath -Directory if (Test-Path $directoryPath) { if ($directories.Count -eq 0) { Write-Host "'$directoryPath' directory is empty." } else { Write-Host "'$directoryPath' directory is not empty. It contains '$($directories.Count)' sub-directories that are '$directories'." } }else{ Write-Host "The Given Path Doesn't Exist." } |
|
1 2 3 |
'E:\Test\Sample Files' directory is not empty. It contains '1' sub-directories that are 'FolderA'. |
We can use the -Recurse parameter to count nested sub-directories from the given directory. See the following example.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$directoryPath = "E:\Test\Sample Files" $directories = Get-ChildItem -Path $directoryPath -Directory -Recurse if (Test-Path $directoryPath) { if ($directories.Count -eq 0) { Write-Host "'$directoryPath' directory is empty." } else { Write-Host "'$directoryPath' directory is not empty. It contains '$($directories.Count)' sub-directories that are '$directories'." } }else{ Write-Host "The Given Path Doesn't Exist." } |
|
1 2 3 |
'E:\Test\Sample Files' directory is not empty. It contains '2' sub-directories that are 'FolderA FolderB'. |
That’s all about how to check if directory is empty in PowerShell.