I want to use Powershell to find all empty directories within a file structure that are called "Information & Specification". I thought I had cracked it when this line
dir -Directory -recurse | Where-Object {$_.Name -eq "Information & Specification" -and (Measure-Object).count -eq 0} | Select-Object -Property Parent
started returning results. However, it looks like it finds all "Information & Specification" folders irrespective of whether they have files in or not - if I reverse the count condition from -eq to -ne I get nothing.
So I set up a simple test hierachy with folders A, B, C at top level. A and B both contain a folder called X. (I am going to search for X rather than "Information & Specification" in my test). Folder C contains folder D, and D contains a folder called X.
I ran
dir -Directory -recurse | Where-Object {$_.Name -eq "X" -and (Measure-Object).count -eq 0} | Select-Object -Property Parent
and got
Parent
......
A
B
D
which appeared correct. But when I created a file in the B/X folder, I still got the same result, so clearly my test for emptiness is wrong. I referred back to Count items in a folder with PowerShell and tried
dir -Directory -recurse | Where-Object {$_.Name -eq "X" -and (Get-ChildItem $_ | Measure-Object).count -eq 0} | Select-Object -Property Parent
but this yields an error message I do not understand
Get-ChildItem : Cannot find path 'C:\temp\search_test\X' because it does not exist.
Can anyone help with the test for emptiness (or an entirely alternative solution?)