Powershell folder size of folders without listing Subdirectories

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

    from sysinternals.com with du.exe or du64.exe -l 1 . or 2 levels down: **du -l 2 c:**

    Much shorter than Linux though ;)

    0 讨论(0)
  • 2021-01-30 22:18

    This is something I wind up looking for repeatedly, even though I wrote myself a nice little function a while ago. So, I figured others might benefit from having it and maybe I'll even find it here, myself. hahaha

    It's pretty simple to paste into your script and use. Just pass it a folder object.

    I think it requires PowerShell 3 just because of the -directory flag on the Get-ChildItem command, but I'm sure it can be easily adapted, if need be.

    function Get-TreeSize ($folder = $null)
    {
        #Function to get recursive folder size
        $result = @()
        $folderResult = "" | Select-Object FolderPath, FolderName, SizeKB, SizeMB, SizeGB, OverThreshold
    
        $contents  = Get-ChildItem $folder.FullName -recurse -force -erroraction SilentlyContinue -Include * | Where-Object {$_.psiscontainer -eq $false} | Measure-Object -Property length -sum | Select-Object sum
        $sizeKB = [math]::Round($contents.sum / 1000,3)   #.ToString("#.##")
        $sizeMB = [math]::Round($contents.sum / 1000000,3)   #.ToString("#.##")
        $sizeGB = [math]::Round($contents.sum / 1000000000,3)   #.ToString("#.###")
    
        $folderResult.FolderPath = $folder.FullName
        $folderResult.FolderName = $folder.BaseName
        $folderResult.SizeKB = $sizeKB
        $folderresult.SizeMB = $sizeMB
        $folderresult.SizeGB = $sizeGB
        $result += $folderResult
    
        return $result
    } 
    
    
    #Use the function like this for a single directory
    $topDir = get-item "C:\test"
    Get-TreeSize ($topDir)
    
    #Use the function like this for all top level folders within a direcotry
    #$topDir = gci -directory "\\server\share\folder"
    $topDir = Get-ChildItem -directory "C:\test"
    foreach ($folderPath in $topDir) {Get-TreeSize $folderPath}  
    
    0 讨论(0)
  • 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" 
    }
    
    0 讨论(0)
  • 2021-01-30 22:22

    My proposal:

    $dir="C:\temp\"
    get-childitem $dir -file -Rec | group Directory | where Name -eq $dir | select Name, @{N='Size';E={(($_.Group.Length | measure -Sum).Sum / 1MB)}}
    
    0 讨论(0)
  • 2021-01-30 22:24

    Interesting how powerful yet how helpless PS can be in the same time, coming from a Nix learning PS. after install crgwin/gitbash, you can do any combination in one commands:

    size of current folder: du -sk .

    size of all files and folders under current directory du -sk *

    size of all subfolders (including current folders) find ./ -type d -exec du -sk {} \;

    0 讨论(0)
  • 2021-01-30 22:25

    Sorry to reanimate a dead thread, but I have just been dealing with this myself, and after finding all sorts of crazy bloated solutions, I managed to come up with this.

    [Long]$actualSize = 0
    foreach ($item in (Get-ChildItem $path -recurse | Where {-not $_.PSIsContainer} | ForEach-Object {$_.FullName})) {
       $actualSize += (Get-Item $item).length
    }
    

    Quickly and in few lines of code gives me a folder size in Bytes, than can easily be converted to any units you want with / 1MB or the like. Am I missing something? Compared to this overwrought mess it seems rather simple and to the point. Not to mention that code doesn't even work since the called function is not the same name as the defined function. And has been wrong for 6 years. ;) So, any reasons NOT to use this stripped down approach?

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