Give this a try:
Get-ChildItem $Path -Recurse -Force |
Where-Object {$_.PSIsContainer -and (Get-ChildItem $_.FullName -Force | Measure-Object).Count -eq 0}
Frage
I am trying to write a PS script to find empty dirs in a directory specified by the parameter. The "empty dirs" should not contain any files and subdirs.
This is the script:
param (
[parameter (mandatory=$true,position=0)]
[string]$Path
)
$dirInfo = get-childitem $Path -recurse | ? {$_.PSIsContainer -eq $True} | % {get-childitem $_} | Measure-Object
$dirInfo | ? {$dirInfo.count -eq 0} | Select-Object Fullname
When I run it on "C:\documents folder" I got the following error:
**get-childitem : Cannot find path 'C:\Documents\ManualScripts\accounts' because it does not exist.
At C:\Documents\ManualScripts\Check-no-file-and-subdir-dir.ps1:5 char:79
+ $dirInfo = get-childitem $Path -recurse | ? {$_.PSIsContainer -eq $True} | % {ge ...
+ ~~
+ CategoryInfo : ObjectNotFound: (C:\Documents\ManualScripts\accounts:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand**
And a lot of this similar errors for each of the subfolders under C:\documents. It append the dir where the script itself is located (C:\Documents\ManualScripts) to the subdir name.
Have done my research and still can not figure out. Any input appreciated. Thanks:)
After the above post, I made some changes on the script but is still not work so far:
param (
[parameter (mandatory=$true,position=0)]
[string]$Path
)
$Dirs = Get-ChildItem $Path -recurse | ? {$_.PSIsContainer -eq $True} | ForEach-Object -Process {$_.FullName} #get list of all directories with whole path
foreach ($Dir in $Dirs) {
$emptyDir = Get-ChildItem $Dir | Measure | where {$_.Count -eq 0} | select-Object
}
Before going to bed, I believe I makes a little progress. And the script looks like this right now:
param (
[parameter (mandatory=$true,position=0)]
[string]$Path
)
$Dirs = Get-ChildItem $Path -recurse | ? {$_.PSIsContainer -eq $True} | ForEach-Object -Process {$_.FullName} #get list of all directories with whole path
#$Dirs | ForEach-Object {Get-ChildItem $_} | ForEach-Object {measure} | where {$_.Count -eq 0} | Select-Object $Dirs
ForEach ($Dir in $Dirs) {
$emptyDir = Get-ChildItem $Dir | Measure | where {$_.Count -eq 0} | Select-Object
$emptyDir
}
If I run the there is something in output (Although is not what I expect)
PS C:\Documents\ManualScripts> .\Check-no-file-and-subdir-dir-rev01.ps1
cmdlet Check-no-file-and-subdir-dir-rev01.ps1 at command pipeline position 1
Supply values for the following parameters:
Path: C:\documents
Count : 0
Average :
Sum :
Maximum :
Minimum :
Property :
Count : 0
Average :
Sum :
Maximum :
Minimum :
Property :
......
......
Something like "Select-Object" picks the output of "measure" not the names of the dirs whose have no files/subdirs underneath.
Lösung
Give this a try:
Get-ChildItem $Path -Recurse -Force |
Where-Object {$_.PSIsContainer -and (Get-ChildItem $_.FullName -Force | Measure-Object).Count -eq 0}