Help needed to determine folder size.

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Yeshaswi Yeshaswi 1 year, 6 months ago.

  • Author
  • #24579
    Profile photo of Yeshaswi

    Hi All,

    I am using the below lines of code to determine the sub-folder sizes under a particular folder

    CODE –

    Function Get-FolderLength


    $objFSO = New-Object -com Scripting.FileSystemObject

    $path = Get-ChildItem -Path 'X:\FolderA\SubFolderB

    foreach($folder in $path){

    [PSCustomObject]@{'Folder Name' = $folder.Name

    "Size" = [math]::round(( ($objFSO.GetFolder($folder.FullName).Size) / 1gb ),2)



    $output = Get-FolderLength | Sort-Object Size -Descending | Select-Object -First 10

    However, this is not helping my cause. The actual sub-folder size is 9.5 GB . Its returning the value as 0. Please help me with this.
    Note – Its is returning correct size for sub-folders lesser than 2.5 GB in size.

    TEST 0

  • #24580
    Profile photo of Matt Bloomfield
    Matt Bloomfield

    Your code actually works OK for me on subfolders 86GB and 13GB in size. I am using Windows 8.1 64-bit and PowerShell version 4.

    I would not do any calculation initially, just check what value you're getting for $objFSO.GetFolder($folder.FullName).Size and make sure it has the correct value in bytes.

    foreach ($folder in $path) {

    If that's not working, then you need to resolve why it can't get the value at that point e.g. permissions, folder path lengths.

    If that works then you know you have a problem with calculating the value for your Size property. Perhaps try specifying the variable type.

    "Size" = [double][math]::round(( ($objFSO.GetFolder($folder.FullName).Size) / 1gb ),2)
  • #24583
    Profile photo of Yeshaswi

    Thanks for the clear description Matt.

    I believe that the problem might be with the folder length paths.
    The particular folder for which the output is failing has lots and lots of subfolders within it.

    Below is the exception i'm getting for that particular folder –

    Exception calling "GetFolder" with "1" argument(s): "Exception from HRESULT: 0x800A004C
    At C:\Libraries\Documents\Sample.ps1:35 char:1
    + [PSCustomObject]@{'Folder Name' = $folder.Name
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

    I am using Windows Server 2008 R2 and PowerShell ver 4

    Please suggest on how to handle this exception.

  • #24585
    Profile photo of Matt Bloomfield
    Matt Bloomfield

    That error may be because you're trying to run the GetFolder() method against a file. I encountered that when testing your script. As you're using PS version 4 you can use the -Directory parameter for the Get-ChildItem command when you set $path. Alternatively, if you're running it on older versions pipe Get-ChildItem to Where-Object {$_.PSIsContainer -eq $true}

    If your problem is due to path lengths that's trickier to overcome. Solutions I have seen for working around the problem include using Robocopy, dynamically mapping drives to reduce path length and enabling long paths with \\?\. I haven't worked with any of those techniques myself though so can't offer solid advice on that.

  • #24616
    Profile photo of Yeshaswi

    Thanks Matt.

    I'll try the Robocopy solution.

You must be logged in to reply to this topic.