Calculating Direcotry Sizes to export to .csv

Hi All,

I am very new to PowerShell but love what I am seeing. I have found a great script exports the folder names, create dates, file path and file type information to a .csv file. What I really need now is to add a column that show the overall folder size including any sub directories. I don’t need the sub directory names just the total for the parent and sub folders.

Can anyone help me add this to the script below? Many thanks for looking.

Here is the script


Get-ChildItem -Path E:\web -Recurse |`
foreach{
$Item = $_
$Type = $_.Extension
$Path = $_.FullName
$Folder = $_.PSIsContainer
$Age = $_.CreationTime

$Path | Select-Object `
    @{n="Name";e={$Item}},`
    @{n="Created";e={$Age}},`
    @{n="filePath";e={$Path}},`
    @{n="Extension";e={if($Folder){"Folder"}else{$Type}}}`
}| 



Export-Csv E:\Web\Results.csv -NoTypeInformation 

hmmm … you have to help me to understand what you mean with “overall folder size” for a file. You want to have a list of files and in one column a folder size? I my opinion that does not fit together.

If you wnat to measure a folder size you can use measure-object. For a give folder it could look like this:

(Get-ChildItem -Path ‘C:\sample’ -Recurse -Force -File |  Measure-Object -Property Length -Sum ).Sum / 1MB

hi there, thank you for the reply. I will explain better.

I have a folder called Web, in there are say three say three folders, each of those contain many sub folders and files of different types. currently my script give me a result like this below in the .csv output which is great I just need one more column for the overall size of folder 1, 2 and 3. So a calculation of all sub folders in these three folders. I added the column below Folder size. Does that help?

Parent folders in my Web folder.

Name Created filePath Extension FOLDER SIZE
folder1 09/10/2017 17:22 E:\web\folder1 Folder ?
folder2 08/08/2017 10:00 E:\web\folder2 Folder ?
folder3 05/06/2017 12:03 E:\web\folder3 Folder ?

The problem with using Measure-Object is that its going to be a pretty slow process if you have a lot of folders. PowerShell doesn’t have a method of directly getting the folder size and you have to count through all of the sub-folders.

If you’re prepared to use an old style VBScript approach you can use the FileSystem COM object like this

function Get-FolderSize {
[CmdletBinding()]
param (
  [string]$path = 'C:\MyData'
)

if (-not (Test-Path -Path $path)){
  Throw "$path - Path not found"
}

$fso = New-Object -ComObject "Scripting.FileSystemObject"

Get-ChildItem -Path $path -Directory -Recurse |
foreach {
  
  $size = ($fso.GetFolder("$($PSItem.FullName)")).Size
  
  $props = [ordered]@{
    Name = $PSItem.Name
    Created = $PSItem.CreationTime
    FilePath = $PSItem.FullName
    SizeMB = [math]::Round( ($size / 1mb), 2)
  }

  New-Object -TypeName PSObject -Property $props
}

}

use as

Get-FolderSize -path

if you want the subfolders immediately after their parent then add a sort

Get-FolderSize -path | sort FilePath

and if you want to order by size then

Get-FolderSize -path | sort SizeMB -Descending