Posts
49
Comments
83
Trackbacks
0
November 2008 Entries
PowerShell version (sort of) of Unix’s du command

PS CTPv2 is finally performant enough with enumerating directories and files that its worthwhile to write deep file system traversal functions. Unix’s du was always incredibly useful for me when managing my files. Here’s the PS equivalent.

   1:  function du ($path = '.\', $unit="MB", $round=0) 
   2:  { 
   3:     get-childitem $path -force | ? { 
   4:       $_.Attributes -like '*Directory*' } | %{ 
   5:          dir $_.FullName -rec -force | 
   6:             measure-object -sum -prop Length | 
   7:                 add-member -name Path -value $_.Fullname -member NoteProperty -pass | 
   8:                     select Path,Count,@{ expr={[math]::Round($_.Sum/"1$unit",$round)}; Name="Size($unit)"} 
   9:          } 
  10:  }

Sample:

[dronagiri] F:\Vivek\>du .\ –unit MB 

Path                                       Count            Size(MB)
----                                       -----            --------
F:\Vivek\Custom UI                           275                  18
F:\Vivek\Desktop                            1151                 114
F:\Vivek\Documents                         20338                3521

 

Hope this is useful for your file management.

posted @ Monday, November 24, 2008 8:33 PM | Feedback (0)
News
A little slow these days as I'm busy working on exchangelabs.com. I will try and post tidbits when I get some time. Enjoy the older posts till then!