# Directory Size Through FileSystemObject

**URL:** <https://forums.powershell.org/t/directory-size-through-filesystemobject/3506>\
**Category:** PowerShell Help\
**Created:** [December 10, 2014, 10:53pm UTC](https://forums.powershell.org/t/directory-size-through-filesystemobject/3506 "2014-12-10T22:53:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![malik-amaduddin](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@malik-amaduddin](https://forums.powershell.org/u/malik-amaduddin)\
**Post date:** [December 10, 2014, 10:53pm UTC](https://forums.powershell.org/t/directory-size-through-filesystemobject/3506/1 "2014-12-10T22:53:54Z")

</div>

The following code snippet to get the directory size of different folders in a local or remote computer. However for some folders (like Windows or Windows.old) it is not calculating the size properly. For other folders it is calculating properly. By properly I mean that comparing the results with Windows Explorer.

$FileSysObj = New-Object -comobject Scripting.FileSystemObject  
$FileSysObj.GetFolder[‘c:\windows.old’].Size  
Remove-Variable FileSysObj

What can be wrong? I have searched and searched and searched. But no clue.

---

<div class="post-metadata">

**Author:** ![sred13](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sred13/32/682_2.png) [@sred13](https://forums.powershell.org/u/sred13)\
**Post date:** [December 11, 2014, 1:19am UTC](https://forums.powershell.org/t/directory-size-through-filesystemobject/3506/2 "2014-12-11T01:19:26Z")

</div>

I’m not sure why that doesn’t work. For folders where you encounter an access denied, you can just use this approach instead.

```
$FileSysObj.GetFolder('c:\windows').SubFolders | % { $sum += $_.Size}; $sum
```

You’re just going to iterate through the .SubFolders property and then select each $FileSysObj.GetFolder.Size property from there and add it to $sum, finally displaying $sum at the end.

---

<div class="post-metadata">

**Author:** ![jeffhicks](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeffhicks/32/301_2.png) [@jeffhicks](https://forums.powershell.org/u/jeffhicks)\
**Post date:** [December 11, 2014, 1:28am UTC](https://forums.powershell.org/t/directory-size-through-filesystemobject/3506/3 "2014-12-11T01:28:15Z")

</div>

You can’t delete Windows.old that way. Too many files and folders have TrustedInstaller as the owner. The best way is to use the DiskCleanup tool and check for System files. If you don’t have a GUI, you can try a PowerShell function I wrote to be used on server core installations. [Welcome • The Lonely Administrator](http://jdhitsolutions.com/blog/2013/10/out-with-the-windows-old/)

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [December 11, 2014, 1:41am UTC](https://forums.powershell.org/t/directory-size-through-filesystemobject/3506/4 "2014-12-11T01:41:48Z")

</div>

Looking at some quick tests

£\> $FileSysObj = New-Object -comobject Scripting.FileSystemObject  
£\> $FileSysObj.GetFolder(‘C:\source’).Size  
41898555590  
£\> $FileSysObj.GetFolder(‘C:\Windows’).Size  
£\> $FileSysObj.GetFolder(“C:\Windows”).Size  
£\> $FileSysObj.GetFolder(‘C:\Windows\Fonts’).Size  
528986695  
£\> $FileSysObj.GetFolder(‘C:\Windows\System32’).Size  
£\> $FileSysObj.GetFolder(‘C:\Windows\System32\WindowsPowerShell’).Size  
41881437

So whats different about those folders?

I think its permission based in that using the COm object you don’t get permissions to list the folder contents and therefore get the size. Also some folders will deny access which could well cause the COM object to stop counting. Think you’ll have to look at those folders another way as suggested above. You may want to just use a pure powershell approach:  
Get-ChildItem -Path c:\windows -Recurse -File -Force | Measure-Object -Sum Length

---

<div class="post-metadata">

**Author:** ![jeffhicks](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeffhicks/32/301_2.png) [@jeffhicks](https://forums.powershell.org/u/jeffhicks)\
**Post date:** [December 11, 2014, 1:52am UTC](https://forums.powershell.org/t/directory-size-through-filesystemobject/3506/5 "2014-12-11T01:52:07Z")

</div>

Oops, looks like I jumped the gun. Sorry you just want a size, most people want to make it go away.

---

<div class="post-metadata">

**Author:** ![malik-amaduddin](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@malik-amaduddin](https://forums.powershell.org/u/malik-amaduddin)\
**Post date:** [December 13, 2014, 8:53pm UTC](https://forums.powershell.org/t/directory-size-through-filesystemobject/3506/6 "2014-12-13T20:53:05Z")

</div>

Thanks for your response. Get-ChildItem -Path c:\windows -Recurse -File -Force | Measure-Object -Sum Length works but is very slow.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:46pm UTC](https://forums.powershell.org/t/directory-size-through-filesystemobject/3506/7 "2024-05-16T20:46:39Z")

</div>

Closed on May 16, 2024, 8:46pm UTC


