Forum Discussion

Abdullah_Shurjeel's avatar
Abdullah_Shurjeel
Copper Contributor
Dec 03, 2020
Solved

Powershell - Retrieve folder size for each folders in the output (Length Property).

My requirement is to retrieve all files from a directory when provided as an input to a certain folder level with the following properties: Name, FullName, Length, LastWriteTime.

 

Current code:

 

$Directory = 'Input'

Get-ChildItem -Path $Directory -Recurse -Depth 2 |
Select-Object Name, FullName, Length, LastWriteTime |

Where-Object {$_.Name -like "*.extension" -or $_.Name -like "*.extension" -or $_.Name -like "extension*"} |
Sort-Object -Property FullName |

Format-Table Name, FullName, Length, LastWriteTime

Current Output:

 

Name             FullName                            Length      LastWriteTime
-----------      ---------------------------      ---------     --------------------
Vol001.zip      C:\Users\ABC\Vol001.zip      100            11/09/2020 11:40:37
Vol002.zip      C:\Users\ABC\Vol002.zip      100            11/09/2020 11:40:37
XY_Vol001      C:\Users\XY_Vol001\                              11/09/2020 11:40:47
XY_Vol002      C:\Users\XY_Vol002\                              11/09/2020 11:41:30

 

I would like the folder size to be provided instead of blank values in Length property field.

I have tried the below by adding

"@{ n = 'Length'; e = {("{0:N8}" -f ((Get-ChildItem -path 'Input' -recurse | Measure-Object -property length -sum ).sum /1MB) + " MB")}}"

like mentioned below:

 

$Directory = 'Input'

Get-ChildItem -Path $Directory -Recurse -Depth 2 |

Select-Object Name, FullName, Length, LastWriteTime,
@{ n = 'Length'; e = {("{0:N8}" -f ((Get-ChildItem -path 'Input' -recurse | Measure-Object -property length -sum ).sum /1MB) + " MB")}} |

Where-Object {$_.Name -like "*.extension" -or $_.Name -like "*.extension" -or $_.Name -like "extension*"} |
Format-Table Name, FullName, Length, LastWriteTime

 

But it is giving parent folder size for all the rows instead of giving their individual sizes.

My guess it would require looping. 

 

Thanks!

  • ChrisBradshaw's avatar
    ChrisBradshaw
    Dec 17, 2020

    Abdullah_Shurjeel It looks like Measure-Object Windows PowerShell is behaving slightly differently to PSCore. I've changed the $Length= .... line to the following which appears to be working as expected.

    $Length= (Get-ChildItem $FileOrFolder.FullName -Recurse | Where-Object {$_.Attributes -notcontains "Directory"} | Measure-Object -Sum -Property Length).Sum

     

4 Replies

  • Abdullah_Shurjeel how about this:

    $Directory = 'Input'
    #Collect a list of all the files and folders
    $FilesAndFolders =Get-ChildItem -Path $Directory -Recurse -Depth 2 
    #Loop through the files and folders
    Foreach ($FileOrFolder in $FilesAndFolders) {
        if ($FileOrFolder.Attributes -contains "Directory") {
            #This is a folder - calculate the total folder size
            $Length= (Get-ChildItem $FileOrFolder -Recurse | Measure-Object -Sum Length).Sum
            $FileOrFolder | Select-Object Name, FullName, @{Name="Length"; Expression={$Length}}, LastWriteTime
        } else {
            #This is a file
            $FileOrFolder | Select-Object Name, FullName, Length, LastWriteTime
        }
    }
    • Abdullah_Shurjeel's avatar
      Abdullah_Shurjeel
      Copper Contributor

      ChrisBradshaw 

       

      I am getting the below error:

       

      Measure-Object : The property "Length" cannot be found in the input for any objects.
      At line:8 char:58
      + ... et-ChildItem $FileOrFolder -Recurse | Measure-Object -Sum Length).Sum
      +                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidArgument: (:) [Measure-Object], PSArgumentException
          + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand

       

      • ChrisBradshaw's avatar
        ChrisBradshaw
        Iron Contributor

        Abdullah_Shurjeel It looks like Measure-Object Windows PowerShell is behaving slightly differently to PSCore. I've changed the $Length= .... line to the following which appears to be working as expected.

        $Length= (Get-ChildItem $FileOrFolder.FullName -Recurse | Where-Object {$_.Attributes -notcontains "Directory"} | Measure-Object -Sum -Property Length).Sum

         

Resources