Powershell folder size of folders without listing Subdirectories

后端 未结 10 2246
北海茫月
北海茫月 2021-01-30 21:40

I have found several resources that use the following script to get folder sizes

$colItems = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContaine         


        
10条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-30 22:21

    The solution posted by @Linga: "Get-ChildItem -Recurse 'directory_path' | Measure-Object -Property Length -Sum" is nice and short. However, it only computes the size of 'directory_path', without sub-directories.
    Here is a simple solution for listing all sub-directory sizes. With a little pretty-printing added.
    (Note: use the -File option to avoid errors for empty sub-directories)

    foreach ($d in gci -Directory -Force) {
      '{0:N0}' -f ((gci $d -File -Recurse -Force | measure length -sum).sum) + "`t`t$d" 
    }
    

提交回复
热议问题