Script: Get-L7FolderSpace.ps1

by 2 users

96 Downloads, 387 Views

1 Comment(s)

Download Embed code

By: George Kesler 08 Apr 2012 12:16 PM UTC in the category: Advanced Event 4

Description:

Get the name and size of each folder under the specified path, including the (root)path itself
The result can be returned (-SortBy parameter):
raw - un-formatted list of folder names and sizes
name - (default) will sort by name like Windows Explorer
Size will sort by the folder size
<#
	.SYNOPSIS
		Get the name and size of each folder under the specified path, including the (root)path itself
		
	.NOTES  Author: George Kesler
	
	.PARAMETER  Path
			The path under which the folders will be listed
			Mandatory. Must be a valid folderpath
			
	.PARAMETER SortBy
			"Name" (default) will sort by name like Windows Explorer 
			"Size" will sort by the folder size
			"Raw" will return unsorted, unformatted data for further analisys 

	.EXAMPLE
		PS C:\SCRIPTS\> .\Get-L7FolderSpace.ps1 -path c:\
		
	.EXAMPLE
		PS C:\SCRIPTS\> .\Get-L7FolderSpace.ps1 c:\ -SortBy size
		
	.EXAMPLE
		PS C:\SCRIPTS\> .\Get-L7FolderSpace.ps1 c:\ -SortBy raw
		
		
#>
param(
	[Parameter(Position=0, Mandatory=$true)]
    [ValidateScript({Test-Path $_ -PathType 'Container'})] 
	[string]$path,
	[ValidateSet("Name","Size","Raw")]
    [String]
    $SortBy = 'Name'
	)

Set-StrictMode -Version 2

function Get-FriendlyUnit{
<#
.SYNOPSIS  Convert numbers into smaller binary multiples
.DESCRIPTION The function accepts a value and will convert it
  into the biggest binary unit available.
.NOTES  Author:  Luc Dekens
.PARAMETER Value
  The value you want to convert.
  This number must be positive.
.PARAMETER IEC
  A switch to indicate if the function shall return the IEC
  unit names, or the more commonly used unit names.
  The default is to use the commonly used unit names.
.EXAMPLE
  PS> Get-FriendlyUnit -Value 123456
.EXAMPLE
  PS> 123456 | Get-FriendlyUnit -IEC
.EXAMPLE
  PS> Get-FriendlyUnit -Value 123456,789123, 45678
#>
  param(
  [CmdletBinding()]
  [parameter(Mandatory = $true, ValueFromPipeline = $true)]
  [double[]]$Value,
  [switch]$IEC
  )

  begin{
    $OldUnits = "B","KB","MB","GB","TB","PB","EB","ZB","YB"
    $IecUnits = "B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"
    if($IEC){$units = $IecUnits}else{$units=$OldUnits}
  }

  process{
    $Value | ForEach-Object{
      if($_ -lt 0){
        write-Error "Numbers must be positive."
        break
      }
      if($value -gt 0){
        $modifier = [math]::Floor([Math]::Log($_,1KB))
      }
      else{
        $modifier = 0
      }
      New-Object PSObject -Property @{
        Value = $_ / [math]::Pow(1KB,$modifier)
        Unit = &{if($modifier -lt $units.Count){$units[$modifier]}else{"1KB E{0}" -f $modifier}}
      }
    }
  }
}

$FolderList = Get-ChildItem $path -Recurse | Where-Object{$_.psiscontainer}
#include the starting folder in the list
$FolderList += Get-Item $path
$FSO = New-Object -com  Scripting.FileSystemObject

$FolderData = $FolderList | ForEach-Object {$FSO.GetFolder($_.fullname) |
Select-Object	@{Name="Folder";Expression={$_.path}},`
				@{Name="SizeOfFolder";Expression={$_.Size}}} 


switch ($SortBy)
	{
	Name { 
		$FolderData |
		Sort-Object { [regex]::Replace($_.Folder, '\d+', { $args[0].Value.PadLeft(20) }) } |
		select folder, @{Name="sizeoffolder"; Expression={$_.sizeoffolder  | Get-FriendlyUnit | ForEach-Object{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}}
		}
	Size {
		$FolderData | sort SizeOfFolder | 
		select folder, @{Name="sizeoffolder"; Expression={$_.sizeoffolder  | Get-FriendlyUnit | ForEach-Object{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}}
		}	
	Raw {
		$FolderData
		}
	}
Top

Comments:

4/8/2012 12:22 PM
Don't know if it'll cost me points or not but I could not resist using Luc's Get-FriendlyUnit function.
http://www.lucd.info/2011/11/06/friendly-units/
Also, I just refuse to do the work necessary to get folders "the PowerShell Way", hence the $FSO = New-Object -com Scripting.FileSystemObject