Powershell test if folder empty

In Powershell, how do I test if a directory is empty?


If you are not interested in hidden or system files you can also use Test-Path

To see if it exists a file in directory .\temp you can use :

Test-Path -Path .\temp\*

or shortly :

Test-Path .\temp\*

Try this...

$directoryInfo = Get-ChildItem C:\temp | Measure-Object
$directoryInfo.count #Returns the count of all of the objects in the directory

If $directoryInfo.count -eq 0, then your directory is empty.


To prevent enumerating each file under c:\Temp (which can be time consuming), we can do somethings like this:

if((Get-ChildItem c:\temp\ -force | Select-Object -First 1 | Measure-Object).Count -eq 0)
{
   # folder is empty
}