Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Directory Size using PowerShell

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 01-30-2008   #1 (permalink)
dm3281
Guest


 

Directory Size using PowerShell

Hello, I have about 45 directories that have about 10 nested directories
with tens of thousands of files in the directory. I have folder compression
turned on since most of my files are .txt and I get a 2:1 compression.

I would like a Powershell script that can recurively scan a root directory
of my choosing and simply give me the total size of each directory in the
root directory, sorted from largest to smallest.

Can anyone help me with this?



My System SpecsSystem Spec
Old 01-31-2008   #2 (permalink)
Shay Levi
Guest


 

Re: Directory Size using PowerShell

Try this:


get-childitem | where {$_.PSIsContainer} | foreach {

$size = (Get-ChildItem $_ -recurse | where {!$_.PSIsContainer} | Measure-Object
-Sum Length).Sum

$obj = new-object psobject
add-member -inp $obj noteproperty path $_.fullName
add-member -inp $obj noteproperty size $size
$obj
}




-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Hello, I have about 45 directories that have about 10 nested
> directories with tens of thousands of files in the directory. I have
> folder compression turned on since most of my files are .txt and I get
> a 2:1 compression.
>
> I would like a Powershell script that can recurively scan a root
> directory of my choosing and simply give me the total size of each
> directory in the root directory, sorted from largest to smallest.
>
> Can anyone help me with this?
>

My System SpecsSystem Spec
Old 01-31-2008   #3 (permalink)
MaciekN
Guest


 

Re: Directory Size using PowerShell

dm3281 pisze:
Quote:

> Hello, I have about 45 directories that have about 10 nested directories
> with tens of thousands of files in the directory. I have folder
> compression turned on since most of my files are .txt and I get a 2:1
> compression.
>
> I would like a Powershell script that can recurively scan a root
> directory of my choosing and simply give me the total size of each
> directory in the root directory, sorted from largest to smallest.
>
> Can anyone help me with this?
>
>
try something like this:

[uint64]$size=0
Get-ChildItem -Recurse | % {$size=$size+$_.Length}
$size
My System SpecsSystem Spec
Old 01-31-2008   #4 (permalink)
Gordon Bell
Guest


 

Re: Directory Size using PowerShell

Also like this...

(gci C:\Folder -recurse | measure-object Length -sum).Sum



dm3281 wrote:
Quote:

> Hello, I have about 45 directories that have about 10 nested directories
> with tens of thousands of files in the directory. I have folder
> compression turned on since most of my files are .txt and I get a 2:1
> compression.
>
> I would like a Powershell script that can recurively scan a root
> directory of my choosing and simply give me the total size of each
> directory in the root directory, sorted from largest to smallest.
>
> Can anyone help me with this?
>
>
My System SpecsSystem Spec
Old 02-02-2008   #5 (permalink)
Keith Hill [MVP]
Guest


 

Re: Directory Size using PowerShell

"dm3281" <dm3281@xxxxxx> wrote in message
news:5A22D438-3110-4295-BFD5-68EEAF6CE32A@xxxxxx
Quote:

> Hello, I have about 45 directories that have about 10 nested directories
> with tens of thousands of files in the directory. I have folder
> compression turned on since most of my files are .txt and I get a 2:1
> compression.
>
> I would like a Powershell script that can recurively scan a root directory
> of my choosing and simply give me the total size of each directory in the
> root directory, sorted from largest to smallest.
>
> Can anyone help me with this?
If you are interested in something that is orders of magnitude faster than
the typical PowerShell solution then go grab DU.exe from the Sysinternals
site:

http://technet.microsoft.com/en-us/s.../bb896651.aspx

Remember folks to use the right tool for the job. The Filesystem provider
performance in v1.0 isn't great and it really shows on issues like this
(especially for large dir structures).

--
Keith

My System SpecsSystem Spec
Old 08-01-2008   #6 (permalink)
Member


Join Date: May 2008
Vista Home Premium 32bit
 
Rep Power: 10
sardinian_guy is just really nicesardinian_guy is just really nicesardinian_guy is just really nicesardinian_guy is just really nice
  sardinian_guy is online now

Re: Directory Size using PowerShell

I was looking for a way to find my 5 biggest folders on my pc.
I've downloaded du utility and ran this command

du -v | sort -desc | select -first 5

This command gives me this result
Totals: .....
Size: .....
Size on disk: ....
Files: ....
Directory ....

So if I want to see these 5 folders I've to write

du -v | sort -desc | select -first 10

Is there a way to skip the five lines I wrote above? (like select between 6 and 10)

Moreover I have 2 partitions on my hard disk and I'd like to know if it's possible to scan them together to find the 5 biggest folders independently of the unit or if I have to run the command twice and then compares the results.
Thanks in advance.
My System SpecsSystem Spec
Old 08-01-2008   #7 (permalink)
Shay Levy [MVP]
Guest


 

Re: Directory Size using PowerShell


You can do array slicing:

(du -v | sort -desc)[6..10]

- or -


$du = du -v | sort -desc
$du[6..$du.length]


---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic



s> I was looking for a way to find my 5 biggest folders on my pc. I've
s> downloaded du utility and ran this command
s>
s> du -v | sort -desc | select -first 5
s>
s> This command gives me this result
s> Totals: .....
s> Size: .....
s> Size on disk: ....
s> Files: ....
s> Directory ....
s> So if I want to see these 5 folders I've to write
s>
s> du -v | sort -desc | select -first 10
s>
s> Is there a way to skip the five lines I wrote above? (like select
s> between 6 and 10)
s>
s> Moreover I have 2 partitions on my hard disk and I'd like to know if
s> it's possible to scan them together to find the 5 biggest folders
s> independently of the unit or if I have to run the command twice and
s> then
s> compares the results.
s> Thanks in advance.


My System SpecsSystem Spec
Old 08-01-2008   #8 (permalink)
Shay Levy [MVP]
Guest


 

Re: Directory Size using PowerShell

Hi sardinian_guy,


You can do array slicing using array ranges:

(du -v | sort -desc)[6..10]

- or -

$du = du -v | sort -desc
$du[6..$du.length]





---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic



s> I was looking for a way to find my 5 biggest folders on my pc. I've
s> downloaded du utility and ran this command
s>
s> du -v | sort -desc | select -first 5
s>
s> This command gives me this result
s> Totals: .....
s> Size: .....
s> Size on disk: ....
s> Files: ....
s> Directory ....
s> So if I want to see these 5 folders I've to write
s>
s> du -v | sort -desc | select -first 10
s>
s> Is there a way to skip the five lines I wrote above? (like select
s> between 6 and 10)
s>
s> Moreover I have 2 partitions on my hard disk and I'd like to know if
s> it's possible to scan them together to find the 5 biggest folders
s> independently of the unit or if I have to run the command twice and
s> then
s> compares the results.
s> Thanks in advance.


My System SpecsSystem Spec
Old 08-01-2008   #9 (permalink)
Member


Join Date: May 2008
Vista Home Premium 32bit
 
Rep Power: 10
sardinian_guy is just really nicesardinian_guy is just really nicesardinian_guy is just really nicesardinian_guy is just really nice
  sardinian_guy is online now

Re: Directory Size using PowerShell

Thank you very much Shay.

(du -v | sort -desc)[6..10]

it's perfect. I didnt' know this trick.

I have a last question. With du I suppose that it's not possibile to use comparison operators, for instance to find all the folders that are greater than a specified size.
I hope I'm wrong.
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
View the size of a directory Lilllis Vista General 7 07-29-2008 12:46 PM
Powershell & Active Directory orphan PowerShell 3 05-03-2007 04:33 AM
Recursive directory size in O(1) yoni Vista file management 0 10-14-2006 11:10 AM
Recursive directory size in O(1) yoni Vista file management 0 10-14-2006 11:09 AM
Windows directory size? =?Utf-8?B?Um9i?= Vista installation & setup 4 09-24-2006 10:48 AM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51