Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating Direcotry Sizes to export to .csv

Status
Not open for further replies.

craigward

Programmer
Nov 13, 2007
230
GB
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

Code:
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
 
Try this (returns in bytes)

Get-ChildItem E:\web -Recurse | Measure-Object -Property Length -Sum


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
I'm not sure if this is what you're looking for or not, but here

Code:
$output = @()
$dirs = @()
$parent = "E:\web"
$output_file = "E:\web\Results.csv"
$dirs = Get-ChildItem -Path $parent -Recurse
foreach ($dir in $dirs)
  {
   $name = $dir.Name
   $path = $dir.FullName
   $created = $dir.CreationTime
   $folder = $dir.PSIsContainer
   $size = (Get-ChildItem -Path $path -Recurse | Measure-Object -Property Length -Sum).Sum
   if ($folder)
     {$type = "Folder"}
   else
     {$type = $dir.Extension}
 
   $item_info = new-object psobject
   $item_info | add-member noteproperty "Name" $name
   $item_info | add-member noteproperty "Path" $path
   $item_info | add-member noteproperty "CreatedOn" $created
   $item_info | add-member noteproperty "Extension" $type
   $item_info | add-member noteproperty "SizeInBytes" $size
 
   $output += $item_info
  }
	
$output | Export-Csv $output_file -NoTypeInformation


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top