Powershell folder size of folders without listing Subdirectories

后端 未结 10 2365
北海茫月
北海茫月 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:37

    This is similar to https://stackoverflow.com/users/3396598/kohlbrr answer, but I was trying to get the total size of a single folder and found that the script doesn't count the files in the Root of the folder you are searching. This worked for me.

    $startFolder = "C:\Users";
    $totalSize = 0;
    
    $colItems = Get-ChildItem $startFolder
    foreach ($i in $colItems)
    {
        $subFolderItems = Get-ChildItem $i.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
        $totalSize = $totalSize + $subFolderItems.sum / 1MB
    
    }
    
    $startFolder + " | " + "{0:N2}" -f ($totalSize) + " MB"
    

提交回复
热议问题