SOLVED

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

%3CLINGO-SUB%20id%3D%22lingo-sub-1951776%22%20slang%3D%22en-US%22%3EPowershell%20-%20Retrieve%20folder%20size%20for%20each%20folders%20in%20the%20output%20(Length%20Property).%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1951776%22%20slang%3D%22en-US%22%3E%3CP%3EMy%20requirement%20is%20to%20retrieve%20all%20files%20from%20a%20directory%20when%20provided%20as%20an%20input%20to%20a%20certain%20folder%20level%20with%20the%20following%20properties%3A%20Name%2C%20FullName%2C%20Length%2C%20LastWriteTime.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECurrent%20code%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24Directory%20%3D%20'Input'%3C%2FP%3E%3CP%3EGet-ChildItem%20-Path%20%24Directory%20-Recurse%20-Depth%202%20%7C%3CBR%20%2F%3ESelect-Object%20Name%2C%20FullName%2C%20Length%2C%20LastWriteTime%20%7C%3C%2FP%3E%3CP%3EWhere-Object%20%7B%24_.Name%20-like%20%22*.extension%22%20-or%20%24_.Name%20-like%20%22*.extension%22%20-or%20%24_.Name%20-like%20%22extension*%22%7D%20%7C%3CBR%20%2F%3ESort-Object%20-Property%20FullName%20%7C%3C%2FP%3E%3CP%3EFormat-Table%20Name%2C%20FullName%2C%20Length%2C%20LastWriteTime%3CBR%20%2F%3E%3CBR%20%2F%3ECurrent%20Output%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EName%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BFullName%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Length%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20LastWriteTime%3CBR%20%2F%3E-----------%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20---------------------------%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20---------%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B--------------------%3CBR%20%2F%3EVol001.zip%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20C%3A%5CUsers%5CABC%5CVol001.zip%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20100%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2011%2F09%2F2020%2011%3A40%3A37%3CBR%20%2F%3EVol002.zip%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20C%3A%5CUsers%5CABC%5CVol002.zip%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20100%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2011%2F09%2F2020%2011%3A40%3A37%3CBR%20%2F%3EXY_Vol001%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20C%3A%5CUsers%5CXY_Vol001%5C%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2011%2F09%2F2020%2011%3A40%3A47%3CBR%20%2F%3EXY_Vol002%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20C%3A%5CUsers%5CXY_Vol002%5C%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2011%2F09%2F2020%2011%3A41%3A30%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20like%20the%20folder%20size%20to%20be%20provided%20instead%20of%20blank%20values%20in%20Length%20property%20field.%3C%2FP%3E%3CP%3EI%20have%20tried%20the%20below%20by%20adding%3C%2FP%3E%3CP%3E%22%40%7B%20n%20%3D%20'Length'%3B%20e%20%3D%20%7B(%22%7B0%3AN8%7D%22%20-f%20((Get-ChildItem%20-path%20'Input'%20-recurse%20%7C%20Measure-Object%20-property%20length%20-sum%20).sum%20%2F1MB)%20%2B%20%22%20MB%22)%7D%7D%22%3C%2FP%3E%3CP%3Elike%20mentioned%20below%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24Directory%20%3D%20'Input'%3C%2FP%3E%3CP%3EGet-ChildItem%20-Path%20%24Directory%20-Recurse%20-Depth%202%20%7C%3C%2FP%3E%3CP%3ESelect-Object%20Name%2C%20FullName%2C%20Length%2C%20LastWriteTime%2C%3CBR%20%2F%3E%40%7B%20n%20%3D%20'Length'%3B%20e%20%3D%20%7B(%22%7B0%3AN8%7D%22%20-f%20((Get-ChildItem%20-path%20'Input'%20-recurse%20%7C%20Measure-Object%20-property%20length%20-sum%20).sum%20%2F1MB)%20%2B%20%22%20MB%22)%7D%7D%20%7C%3C%2FP%3E%3CP%3EWhere-Object%20%7B%24_.Name%20-like%20%22*.extension%22%20-or%20%24_.Name%20-like%20%22*.extension%22%20-or%20%24_.Name%20-like%20%22extension*%22%7D%20%7C%3CBR%20%2F%3EFormat-Table%20Name%2C%20FullName%2C%20Length%2C%20LastWriteTime%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20it%20is%20giving%20parent%20folder%20size%20for%20all%20the%20rows%20instead%20of%20giving%20their%20individual%20sizes.%3C%2FP%3E%3CP%3EMy%20guess%20it%20would%20require%20looping.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1951776%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1992957%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20-%20Retrieve%20folder%20size%20for%20each%20folders%20in%20the%20output%20(Length%20Property).%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1992957%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F885996%22%20target%3D%22_blank%22%3E%40Abdullah_Shurjeel%3C%2FA%3E%26nbsp%3Bhow%20about%20this%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24Directory%20%3D%20'Input'%0A%23Collect%20a%20list%20of%20all%20the%20files%20and%20folders%0A%24FilesAndFolders%20%3DGet-ChildItem%20-Path%20%24Directory%20-Recurse%20-Depth%202%20%0A%23Loop%20through%20the%20files%20and%20folders%0AForeach%20(%24FileOrFolder%20in%20%24FilesAndFolders)%20%7B%0A%20%20%20%20if%20(%24FileOrFolder.Attributes%20-contains%20%22Directory%22)%20%7B%0A%20%20%20%20%20%20%20%20%23This%20is%20a%20folder%20-%20calculate%20the%20total%20folder%20size%0A%20%20%20%20%20%20%20%20%24Length%3D%20(Get-ChildItem%20%24FileOrFolder%20-Recurse%20%7C%20Measure-Object%20-Sum%20Length).Sum%0A%20%20%20%20%20%20%20%20%24FileOrFolder%20%7C%20Select-Object%20Name%2C%20FullName%2C%20%40%7BName%3D%22Length%22%3B%20Expression%3D%7B%24Length%7D%7D%2C%20LastWriteTime%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%23This%20is%20a%20file%0A%20%20%20%20%20%20%20%20%24FileOrFolder%20%7C%20Select-Object%20Name%2C%20FullName%2C%20Length%2C%20LastWriteTime%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1996556%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20-%20Retrieve%20folder%20size%20for%20each%20folders%20in%20the%20output%20(Length%20Property).%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1996556%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F9929%22%20target%3D%22_blank%22%3E%40Chris%20Bradshaw%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20getting%20the%20below%20error%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EMeasure-Object%20%3A%20The%20property%20%22Length%22%20cannot%20be%20found%20in%20the%20input%20for%20any%20objects.%0AAt%20line%3A8%20char%3A58%0A%2B%20...%20et-ChildItem%20%24FileOrFolder%20-Recurse%20%7C%20Measure-Object%20-Sum%20Length).Sum%0A%2B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20~~~~~~~~~~~~~~~~~~~~~~~~~~%0A%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20InvalidArgument%3A%20(%3A)%20%5BMeasure-Object%5D%2C%20PSArgumentException%0A%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20GenericMeasurePropertyNotFound%2CMicrosoft.PowerShell.Commands.MeasureObjectCommand%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

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!

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
    }
}

@Chris Bradshaw 

 

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

 

best response confirmed by Abdullah_Shurjeel (Occasional Contributor)
Solution

@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

 

Thanks Chris for the response.
The code is working now.