Table of Contents [hide]
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.bat
andtestPS.ps1
files, 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.txt
and a folder namedFolderA
, which also containsfile.txt
file andFolderB
folder. TheFolderB
is 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.
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 $directoryPath
that 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
-Path
parameter and mention$directoryPath
without it asGet-ChildItem $directoryPath
; it will still work as desired.
Let’s practice the same code example below for an empty directory.
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.
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.
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 -First
parameter 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-ChildItem
while writing this article. Let’s learn them one by one.
Use gci
with the Count
property to check if directory is empty in PowerShell.
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.
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.
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.
Now, specify a random path which does not exist. See the following example to observe the code and its output.
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-Path
cmdlet also tells if the specified path syntax is correct. It returnsFalse
if the path is a whitespace or an empty string. It returns a non-terminating error if the path is$null
, an array of$null
or 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-ChildItem
cmdlet to get all files from the specified path and store them in a variable. - Use
Test-Path
within theif
statement to check if the given path exists. Display a message if it doesn’t exist.- If the path exists, use the nested
if
to compare a variable’sCount
property (variable created in the first step) with0
using the-eq
operator. - 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
We can use the -Recurse
parameter to count files from the subdirectories. See the following example.
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-ChildItem
cmdlet to get all sub-directories from the specified path and store them in a variable. - Use
Test-Path
within theif
statement to check if the given path exists. Display a message if it doesn’t exist.- If the path exists, use the nested
if
to compare a variable’sCount
property (variable created in the first step) with0
using the-eq
operator. - 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
We can use the -Recurse
parameter to count nested sub-directories from the given directory. See the following example.
That’s all about how to check if directory is empty in PowerShell.