PowerShell Format-DiskSpace Function

Create a PowerShell Function to Format DiskspacePowerShell Function Format-DiskSpace Display

The purpose of this script is to make it easier to read large numbers, for instance, to format 3295002640005 as 3GB.

PowerShell Format-DiskSpace Function

Large Number Format Problem

It's difficult to get a 'feel' for large numbers, this is particularly true if the raw value is in bytes – a common scenario when measuring disk or file sizes.

Even after adding a thousand separator it's still difficult to estimate if 230993200055 is approximately 200 Gigabytes, or 2 Terabytes.  If you relied on guestimation when buying a disk you could make an expensive mistake!

How To Format DiskSize as TB, GB, or MB

Pure PowerShell logic:
This script identifies the range of the input by employing the standard PowerShell 'If' and 'ElseIf' constructions, for example, it starts by checking 'If' the number is greater than, or equal to, 1 terabyte:- If ($Type -ge 1TB).

# PowerShell Script to Display File Size
Function Format-DiskSize() {
Param ([long]$Type)
If ($Type -ge 1TB) {[string]::Format("{0:0.00} TB", $Type / 1TB)}
ElseIf ($Type -ge 1GB) {[string]::Format("{0:0.00} GB", $Type / 1GB)}
ElseIf ($Type -ge 1MB) {[string]::Format("{0:0.00} MB", $Type / 1MB)}
ElseIf ($Type -ge 1KB) {[string]::Format("{0:0.00} KB", $Type / 1KB)}
ElseIf ($Type -gt 0) {[string]::Format("{0:0.00} Bytes", $Type)}
Else {""}
} # End of function

$BigNumber = "230993200055"

Format-DiskSize $BigNumber

Suggestion:  Try typing a number for example:
Format-DiskSize 21911934994

A Better Suggestion: Add a Read-Host command to capture the disksize:
$BigNumber = Read-Host "Enter disk size"
Format-DiskSize $BigNumber

Note 1:  The rhythm of my function is
'Function' – Declares the script block that follows.
'Param' – The key feature of this Format-DiskSize function.
It helps understanding to trace the structure: Function <name> {Param () }

Note 2: I declared the $Type parameter type as [long].  You could also try [decimal].  However, I found that [int] could not cope with values in the gigabyte range, that is why I recommend [long] or [decimal].

PowerShell Script to Display Disk Volume Size Neatly

Here is a script that employs Get-WmiObject to retrieve information about the size of the various volumes on the machine from where you run this script.

First, let us investigate the problem with the default number format:

# PowerShell Script to Retrieve Disk Volumes
Write-Host "Calculating Volume Sizes …"
$YourDisk = Get-WmiObject Win32_volume
ForEach ($Item in $YourDisk) {
"{0,-8} {1,-20}" -f $Item.Name, $Item.Capacity

# It's difficult to interpret the numbers in this output:
C:\ 262143995904
D:\ 213768990720
E:\ 314466889728
F:\ 367001595904
G:\ 262143995904
H:\ 262143995904

Second, my solution:  Format-DiskSize Function

# PowerShell Script to Display Volume Sizes Neatly
Function Format-DiskSize() {
Param ([decimal]$Type)
If ($Type -ge 1TB) {[string]::Format("{0:0} TB", $Type / 1TB)}
ElseIf ($Type -ge 1GB) {[string]::Format("{0:0} GB", $Type / 1GB)}
ElseIf ($Type -ge 1MB) {[string]::Format("{0:0} MB", $Type / 1MB)}
ElseIf ($Type -ge 1KB) {[string]::Format("{0:0} KB", $Type / 1KB)}
ElseIf ($Type -gt 0) {[string]::Format("{0:0} Bytes", $Type)}
Else {""}
} # End of Function Format-DiskSize

#Section to utilize newly-created function.
$YourDisk = Get-WmiObject Win32_volume
ForEach ($Item in $YourDisk) {
"{0,-8} {1,2}" -f $Item.Name, (Format-DiskSize $Item.Capacity)

Neatly formatted
C:\  244 GB
D:\  199 GB
E:\  292 GB
F:\  341 GB
G:\  244 GB
H:\  244 GB

Note 3: To see the effect of our formatting function, edit the script thus:
Replace: (Format-DiskSize $Item.Capacity)
With plain: $Item.Capacity

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Function to Format File Length as MB, KB, or Bytes

Here is another job for a function, namely to format the numbers so they show a meaningful format, it also displays units that vary depending on the number of digits in the raw file length.

# Script to Format File Length
# N.B. Requires PowerShell 3.0 or later.

Function Format-FileSize() {
Param ([decimal]$Type)
If ($Type -ge 1GB) {[string]::Format("{0:0} GB ", $Type / 1GB)}
ElseIf ($Type -ge 1MB) {[string]::Format("{0:0} MB ", $Type / 1MB)}
ElseIf ($Type -ge 1KB) {[string]::Format("{0:0} KB ", $Type / 1KB)}
ElseIf ($Type -gt 0) {[string]::Format("{0:0} Bytes", $Type)}
Else {""}
} # End of function

$Cdrive = Get-ChildItem C:\ -Force -File | Sort-Object length -Descending
ForEach ($File in $Cdrive) {
"{0,-28} {1, 9}" -f $File.Name, (Format-FileSize $File.Length)

Note 4: The -File parameter requires PowerShell 3.0.  However, if you have a previous version, try this:

$Cdrive = Get-ChildItem C:\ -force | Where {$_.Attributes -Notmatch 'Directory'}

Note 5: This script focuses on the C:\;  You could amend the source to C:\Windows.

#Results, observe the units
pagefile.sys        8 GB
hiberfil.sys         6 GB
version             16 KB
user.js            450 Bytes
Program.txt     399 Bytes
dvmexp.idx    177 Bytes
splash.idx        68 Bytes

See more on PowerShell functions »

Summary of PowerShell Function Format DiskSize

It can be difficult to read large numbers, for instance to format 3295002640005 as 3GB.  Here is a classic job for a PowerShell function to display the disk size bytes as MB, GB, or even Terabytes.

If you like this page then please share it with your friends


See More Windows PowerShell Examples Functions

Scripting PowerShell Function   • PowerShell Create Shortcut   • PowerShell Function Shortcut

PowerShell Tutorials  • PowerShell Temp  • PowerShell Get-Item Env:  • Clear-WinEvent Function

Create PowerShell Function  • PowerShell Examples  • PowerShell Function Format DiskSize

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.