Powershell folder size of folders without listing Subdirectories

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

提交回复
热议问题