0

I am trying to write a script using powershell to get folder / file size as mentioned below

$StartFolder = "D:\"
$Output = "C:\Temp\test-d.csv"

Add-Content -Value "Folder Path|Size" -Path $Output

$colItems = (Get-ChildItem $startFolder -Recurse | Measure-Object -Property Length -Sum)
"$StartFolder -- " + "{0:N2}" -f ($colItems.Sum / 1MB) + " MB" # | Out-File $Output

$colItems = (Get-ChildItem $startFolder -Recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems) {
    $subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object -Property Length -Sum)
    $i.FullName + "|" + "{0:N2}" -f ($subFolderItems.Sum / 1MB) + " MB" | Out-File $Output -Append
}

I am getting error as mentioned below:

Measure-Object : The property "Length" cannot be found in the input for any
objects.
At line:12 char:65
+         $subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object - ...
+
    + CategoryInfo          : InvalidArgument: (:) [Measure-Object], PSArgumentException
    + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand

Measure-Object : The property "Length" cannot be found in the input for any
objects.
At line:12 char:65
+         $subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object - ...
+
    + CategoryInfo          : InvalidArgument: (:) [Measure-Object], PSArgumentException
    + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand

Also, when I target C: drive I am getting "access denied" on some system files:

Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\WMI\RtBackup'
is denied.
At line:12 char:28
+         $subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object - ...
+                            
    + CategoryInfo          : PermissionDenied: (C:\Windows\Syst...es\WMI\RtBackup:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
2
  • If you are not an admin or do not have privileges , you wont be able to modify system directories like that. So either you can try with run as admin or give necessary permissions. since its not having access, it is not able to pick the length. Commented Jul 18, 2017 at 3:33
  • Why the hell are you concatenating table viewl with pipes like that? PowerShell has built in support for csv, json and there are modules for excel as well... Commented Jul 18, 2017 at 7:38

1 Answer 1

3

DirectoryInfo objects don't have a property Length, so you need to restrict your size calculation to files.

$colItems = Get-ChildItem $startFolder -Recurse -Force |
            Where-Object { -not $_.PSIsContainer } |
            Measure-Object -Property Length -Sum

It might be easier to use a Scripting.FileSystemObject COM object, because that will allow you to get directory objects with the size of their content. And you may want to use Export-Csv for exporting your data to a CSV. Use calculated properties for building the required objects.

$fso = New-Object -COM 'Scripting.FileSystemObject'

$folders = @($StartFolder)
$folders += Get-ChildItem $StartFolder -Recurse -Force |
            Where-Object { $_.PSIsContainer } |
            Select-Object -Expand FullName

$folders |
    Select-Object @{n='Path';e={$_}}, @{n='Size';e={$fso.GetFolder($_).Size}} |
    Export-Csv $Output -Delimiter '|' -NoType
Sign up to request clarification or add additional context in comments.

7 Comments

Slight syntax error in the select-object. Should be a comma instead of a semicolon separating the two items: Select-Object @{n='Path';e={$_}}, @{n='Size';e={$fso.GetFolder($_).Size}} |
Indeed. Thanks for the heads up. Fixed.
@ Ansgar Wiechers & Itchydon, thanks for your reply and my apologies for delayed reply. I am also looking into get the size of the hidden files as well along with other files size...Is there a way we can do, can you pls let me know...
Get-ChildItem -Force
Tried both - Get-ChildItem $StartFolder -Recurse -Force & Get-ChildItem -Force $StartFolder -Recurse
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.