A Simple PowerShell Windows du Implementation

I was looking for a quick and easy way to get directory and file size for a given directory and its sub-directories.  I came across a nice post by Luis Diego Fallus and ended up using a commenter’s code to produce output in MB format.

gci . | %{$f=$_; gci -r $_.FullName |
	measure-object -property length -sum |
		select @{Name="Name"; Expression={$f}} ,
		@{Name="Sum (MB)"; Expression={ "{0:N3}" -f ($_.sum / 1MB) }}, Sum } |
	sort Sum -desc | format-table -Property Name,"Sum (MB)" -autosize

Example Output

Name                  Sum (MB)
----                  --------
Adobe                 1,603.809
TestDirectory         67.715

Published by

Rich

Just another IT guy.

Leave a Reply

Your email address will not be published. Required fields are marked *