INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Calculating Direcotry Sizes to export to .csv

Calculating Direcotry Sizes to export to .csv

(OP)
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 

RE: Calculating Direcotry Sizes to export to .csv

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.

RE: Calculating Direcotry Sizes to export to .csv

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close