Getting a count from Get-ChildItem can provide false results because an empty folder or error accessing a folder could result in a 0 count.
The way I check for empty folders is to separate out errors:
Try { # Test if folder can be scanned
$TestPath = Get-ChildItem $Path -ErrorAction SilentlyContinue -ErrorVariable MsgErrTest -Force | Select-Object -First 1
}
Catch {}
If ($MsgErrTest) { "Error accessing folder" }
Else { # Folder can be accessed or is empty
"Folder can be accessed"
If ([string]::IsNullOrEmpty($TestPath)) { # Folder is empty
" Folder is empty"
}
}
The above code first tries to acces the folder. If an error occurs, it outputs that an error occurred. If there was no error, state that "Folder can be accessed", and next check if it's empty.
This will first check for a valid directory (Test-Path ($dir)). It will then check for any contents including any directories, hidden file, or "regular" files** due to the attributes d, h, and a, respectively.
** Actually I'm not 100% certain what the defined effect of of a is here, but it does in any case cause all files to be included. The documentation states that ABC1 shows hidden files, and I believe ABC2 should show system files, so I'm guessing a on it's own just shows "regular" files. If you remove it from the function above, it will in any case find hidden files, but not others.
I post this because I was having edge-case issues with names that contained brackets [ ]; failure was when using other methods and the output piped to Remove-Item missed the directory names with brackets.