Powershell folder size of folders without listing Subdirectories

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

    This simple solution worked for me as well.

    powershell -c "Get-ChildItem -Recurse 'directory_path' | Measure-Object -Property Length -Sum"
    
    0 讨论(0)
  • 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"
    
    0 讨论(0)
  • 2021-01-30 22:38

    You need to get the total contents size of each directory recursively to output. Also, you need to specify that the contents you're grabbing to measure are not directories, or you risk errors (as directories do not have a Length parameter).

    Here's your script modified for the output you're looking for:

    $colItems = Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $true} | Sort-Object
    foreach ($i in $colItems)
    {
        $subFolderItems = Get-ChildItem $i.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
        $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
    }
    
    0 讨论(0)
  • 2021-01-30 22:39

    At the answer from @squicc if you amend this line: $topDir = Get-ChildItem -directory "C:\test" with -force then you will be able to see the hidden directories also. Without this, the size will be different when you run the solution from inside or outside the folder.

    0 讨论(0)
提交回复
热议问题