Param ( | |
[parameter ( | |
Mandatory=$true | |
, position=0 | |
, HelpMessage="Please enter folder name" | |
) | |
] | |
[alias("f")] | |
[string] $folder = "." | |
) | |
<# | |
Set strict mode | |
#> | |
Set-StrictMode -Version 1.0 | |
[string] $log=""; | |
[object] $objFSItems=$Null; | |
[long] $lNumberofFiles=0; | |
[long] $_total=0; | |
[long] $_grandtotal=0; | |
[object] $objListofFolders=$Null; | |
[string] $folderDetail=""; | |
[string] $formatDetail=""; | |
[string] $formatTotal=""; | |
[long] $_value = 0; | |
[long] $_sizeMB = 0; | |
[long] $_grandTotal = 0; | |
$folderDetail = "Folder => {0}"; | |
$formatDetail = "`t Size = {0:n3} {1}"; | |
$formatTotal = "`r`n`r`nTotal Size = {0:n3} {1}"; | |
#validate folder existence | |
$folderExist = test-path -isvalid $folder; | |
#If folder does not exists, say SO | |
if ($folderExist -eq $False) | |
{ | |
$log = "Folder {0} does not exist" -f $folder; | |
Write-Warning $log | |
exit | |
} | |
#Get folder and file list | |
$objFSItems = Get-ChildItem $folder -recurse -Force -ErrorAction SilentlyContinue; | |
# Process file size | |
$lNumberofFiles =0; | |
$objListofFolders=@{}; | |
# Iterate files | |
$objFSItems | Foreach-Object { | |
# Number of files | |
$lNumberofFiles = $lNumberofFiles +1; | |
# Get full file name | |
$_fileNameFull = $_.FullName; | |
# Get file name | |
$_fileName = $_.Name; | |
#get folder name | |
$_folderName = $($_fileNameFull | split-path -parent) | |
#is container | |
$_isContainer = $_.PSIsContainer; | |
#get lenght | |
$_length = $_.Length; | |
if ($_isContainer -eq $True) | |
{ | |
#is container registered | |
$objItem = $objListofFolders.Item($_folderName); | |
if ($objItem -eq $Null) | |
{ | |
$objListofFolders.Add($_folderName, 0); | |
} | |
} | |
if ($_isContainer -eq $False) | |
{ | |
$_total = $objListofFolders[$_folderName]; | |
$objListofFolders.Set_Item($_folderName, $_total + $_length); | |
} | |
} | |
$_grandtotal =0; | |
$objListofFolders.GetEnumerator() | Sort-Object Name | ForEach-Object{ | |
$_value = $($_.value); | |
$_sizeMB = $($_.value) / ( 1MB); | |
$_grandTotal = $_grandTotal + ($_.value); | |
$log = $folderDetail -f $($_.key); | |
Write-Output $log | |
<# | |
Only show sizes for folders that have files | |
#> | |
if ($_value -gt 0) | |
{ | |
Write-Output ($formatDetail -f $_sizeMB, "MB"); | |
} | |
} | |
$_grandTotalGB = $_grandTotal / ( 1GB); | |
$log = $formatTotal -f $_grandTotalGB, "GB"; | |
Write-Host $log -ForegroundColor Blue; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment