Scripting Games 2012 Advanced Event 4

Advanced Event 4:


function HumanReadSize
{
    Param ([long]$Size)
if ($Size -ge 1Gb )
{
return ("{0:N2}" -f ($Size / 1Gb) + " GigaBytes")
}
elseif ($Size -ge 1Mb )
{
return ("{0:N2}" -f ($Size / 1Mb ) + " MegaBytes")
}
elseif ($Size -ge 1Kb )
{
return ("{0:N2}" -f ($Size / 1Kb ) + " KiloBytes")
}
elseif (($Size -ge 0 ) -and ($Size -lt 1Kb))
{
return ("{0:N0}" -f ($Size) + " Bytes")
}
}

function Get-FolderSize {
<#
.SYNOPSIS
    This script reports information about folder space.
.DESCRIPTION
    This script reports information about folder space.
.LINK
    http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/05/the-2012-scripting-games-advanced-event-4-determine-folder-space.aspx 
.PARAMETER Path
    Path of the root folder.
.EXAMPLE
    "c:\windows"| get-foldersize 
.EXAMPLE
    get-foldersize "c:\windows" | Sort-Object Folder
#>

[CmdletBinding()]
param([Parameter(Mandatory = $true, ValueFromPipeline = $true)][string]$path)

BEGIN {}

PROCESS {
if (!(test-path $path))
{
Write-Output "Folder does not exist"
return
}
$f=@()
    #Calculating the root folder size
     $rootItems = (Get-ChildItem $path -recurse -Force | Measure-Object -property length -sum)
     $sumroot = HumanReadSize $rootItems.sum
    
        $obj  = New-Object -TypeName PSObject
        $obj | Add-Member -MemberType NoteProperty -Name Folder -Value $path
        $obj | Add-Member -MemberType NoteProperty -Name "Size of Folder" -value $sumroot
        $f +=$obj
    #Calculating the root subfolders size
$colItems = (Get-ChildItem $path -recurse | Where-Object {$_.PSIsContainer -eq $True} )
foreach ($i in $colItems)
    {
        $subFolItems = (Get-ChildItem $i.FullName -recurse -Force | Measure-Object -property length -sum)
        $sumsubfol = HumanReadSize $subFolItems.sum
     
        $obj  = New-Object -TypeName PSObject
        $obj | Add-Member -MemberType NoteProperty -Name Folder -Value $i.FullName
        $obj | Add-Member -MemberType NoteProperty -Name "Size of Folder" -value $sumsubfol
        $f +=$obj
    }
   
           Write-Output $f
}
END {}
}

Comments

Popular posts from this blog

How to convert string to Base64 and vice versa using Powershell

Machine domain group policy failed to apply

Install Windows Server 2012 on HP DL 380 G5