Need help to get format size show GB, MB or KB

rated by 0 users
This post has 9 Replies | 2 Followers

Top 100 Contributor
Posts 28
jbvu2k Posted: 12-09-2010 11:40 AM

I have this PS script below but can't seem to get the format for Directory zise to show in GB, MB or KB to work properly

Param(
 [string]$path = "C:\TEST",
 [int]$first = 100
)# end param
# *** Function Here ***

function Get-DirSize ($path){

  BEGIN {}
 
  PROCESS{
    $size = 0
    $folders = @()
 
    foreach ($file in (Get-ChildItem $path -Force -ea SilentlyContinue)) {
      if ($file.PSIsContainer) {
        $subfolders = @(Get-DirSize $file.FullName)
        $size += $subfolders[-1].Size
        $folders += $subfolders
      } else {
        $size += $file.Length
      }
    }
 
    $object = New-Object -TypeName PSObject
    $object | Add-Member -MemberType NoteProperty -Name Folder `
                         -Value (Get-Item $path).FullName
    $object | Add-Member -MemberType NoteProperty -Name Size -Value $size
    $folders += $object
    Write-Output $folders
  }
 
  END {}
} # end function Get-DirSize

Function Get-FormattedNumber($size)
{
  IF($size -ge 1GB)
   {
      "{0:n2}" -f  ($size / 1GB) + "GB"
   }
 ELSEIF($size -ge 1MB)
    {
      "{0:n2}" -f  ($size / 1MB) + "MB"
    }
 ELSE
    {
      "{0:n2}" -f  ($size / 1KB) + "KB"
    }
} #end function Get-FormattedNumber

 # *** Entry Point to Script ***
 
 if(-not(Test-Path -Path $path))
   {
     Write-Host -ForegroundColor red "Unable to locate $path"
     Help $MyInvocation.InvocationName -full
     exit
   }
 Get-DirSize -path $path |
 Sort-Object -Property size -Descending |
 Select-Object -Property folder, size -First $first |
 Export-csv C:\TEST.csv |
 Format-Table -Property Folder,
  @{ Label="Size of Folder" ; Expression = {Get-FormattedNumber($_.size)} }

===END

and the output for this is

Folder Size
C:\TEST 353429591
C:\TEST\Winternals 2003 77379493
C:\TEST\Microsoft 63814663
C:\TEST\Microsoft\Outlook 12523418
C:\TEST\MS_Hotfix 11306464
C:\TEST\IS 10015969
C:\TEST\Printers 7806376
C:\TEST\Citrix 3835890
C:\TEST\MS_Hotfix\Install 3522545
C:\TEST\Rightfax94 2759570
C:\TEST\MS_Hotfix\983458 2431753
C:\TEST\IS\PS 2172705
C:\TEST\MS_Hotfix\KB980568-x64 1583001
C:\TEST\WIP 493983
C:\TEST\DRIVE 489729
C:\TEST\Report 423936
C:\TEST\Rightfax94\Images 385551
C:\TEST\IS\TCPViewer 238256
C:\TEST\Help 127156
C:\TEST\IS\PS\Logs 66411
C:\TEST\Logs 54863
C:\TEST\C2 51886
C:\TEST\IS\Install 35336
C:\TEST\ITK 30357

 

 

Not Ranked
Posts 5
Hi, I use the following function to format my report output. I must be honest, I didn't write it, but I can't remember where I found it: Hope it helps. Function Get-OptimalSize() ############################################################################### # Description: Convert Bytes into the appropriate unit of measure # Author: Unknown # Last Update: 5/08/2010 2:03:57 PM # Arguments: [int64] The byte value to be converted # Returns: [string] Display friendly value ############################################################################### { Param([int64]$sizeInBytes) switch ($sizeInBytes) { {$sizeInBytes -ge 1TB} {"{0:n$sigDigits}" -f ($sizeInBytes/1TB) + " TB" ; break} {$sizeInBytes -ge 1GB} {"{0:n$sigDigits}" -f ($sizeInBytes/1GB) + " GB" ; break} {$sizeInBytes -ge 1MB} {"{0:n$sigDigits}" -f ($sizeInBytes/1MB) + " MB" ; break} {$sizeInBytes -ge 1KB} {"{0:n$sigDigits}" -f ($sizeInBytes/1KB) + " KB" ; break} Default { "{0:n$sigDigits}" -f $sizeInBytes + " Bytes" } } # EndSwitch } # End Function Get-OptimalSize
Not Ranked
Posts 5
Sorry, my first code post, and I'm not sure how that happened.
Not Ranked
Posts 5

Sorry, my first code post, and I'm not sure how that happened.

Not Ranked
Posts 5

Sorry, I'll try again:

Function Get-OptimalSize()
###############################################################################
# Description:  Convert Bytes into the appropriate unit of measure
# Author:       Unknown
# Last Update:  5/08/2010 2:03:57 PM
# Arguments:    [int64] The byte value to be converted
# Returns:      [string] Display friendly value
###############################################################################
{
    Param([int64]$sizeInBytes)
    switch ($sizeInBytes)
    {
        {$sizeInBytes -ge 1TB} {"{0:n$sigDigits}" -f ($sizeInBytes/1TB) + " TB" ; break}
        {$sizeInBytes -ge 1GB} {"{0:n$sigDigits}" -f ($sizeInBytes/1GB) + " GB" ; break}
        {$sizeInBytes -ge 1MB} {"{0:n$sigDigits}" -f ($sizeInBytes/1MB) + " MB" ; break}
        {$sizeInBytes -ge 1KB} {"{0:n$sigDigits}" -f ($sizeInBytes/1KB) + " KB" ; break}
        Default { "{0:n$sigDigits}" -f $sizeInBytes + " Bytes" }
    } # EndSwitch
} # End Function Get-OptimalSize

Top 100 Contributor
Posts 28

Thanks Spl1nt4 for reply with Function. I still have the same output with Export-csv. Without Export-csv, i will get the result in MB as expected. Still trying , and trying .... to figure out how to pipe the result function to Output.

Top 10 Contributor
Posts 636
Microsoft MVP
Top Contributor

At the end of your function, you currently do this:

Select-Object -Property folder, size -First $first |
 Export-csv C:\TEST.csv |
 Format-Table -Property Folder,
  @{ Label="Size of Folder" ; Expression = {Get-FormattedNumber($_.size)} }

Try this instead:

Select-Object -Property folder, size -First $first |
Select-Object Folder,
  @{ Label="Size of Folder" ; Expression = {Get-FormattedNumber($_.size)} } |
 Export-csv C:\TEST.csv 

Format-Table will not do you any good because you apply it AFTER you exported, and also Format-Table will change objects to formatting objects that you no longer can export. Select-Object accepts the same property parameters but keeps the objects, so by adding that BEFORE you export, all should be fine.

 

 

Top 500 Contributor
Posts 10

Another function for converting, lets you specify the decimal precision:

function to-kmg { 
param ($bytes,$precision='0'
foreach ($i in ("Bytes","KB","MB","GB","TB")) { 
if (($bytes -lt 1000) -or ($i -eq "TB")){ 
$bytes = ($bytes).tostring("F0" + "$precision"
return $bytes + " $i" 

else {$bytes /= 1KB} 

Top 500 Contributor
Posts 10

Sorry, dropped the last 2 braces:

function to-kmg { 
param ($bytes,$precision='0'
foreach ($i in ("Bytes","KB","MB","GB","TB")) { 
if (($bytes -lt 1000) -or ($i -eq "TB")){ 
$bytes = ($bytes).tostring("F0" + "$precision"
return $bytes + " $i" 

else {$bytes /= 1KB} 

}
Top 100 Contributor
Posts 28

Great catch Tobias... Appreciated your help and time.

JB

Page 1 of 1 (10 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.