I scanned a recent forum post on PowerShell.org where the user seemed (again, I scanned it) to want to run some files through Get-ChildItem and Select-Object, and report the size in something other than the bytes default. It didn’t appear he, or she, wanted all the files in one of the measurement groups: B, KB, MB, or GB, but instead in their proper measurement group, dependent on the file’s size.
That may be hard to understand at first read, so let me try and explain it another way. If a file has less than 1,024 bytes, then it should be reported in bytes, if it has 1,024 – 1,048,575 bytes, then it should be reported in Kilobytes, if it has 1,048,576 – 1,073,741,824 bytes then it should be reported Megabytes, and if it has 1,073,741,825 or more bytes, then it should be reported in Gigabytes.
I wrote a long, “one-liner” to do this and have included it, and some sample output, below. I don’t profess to guarantee this doesn’t have any errors, so if you find some, then let me know. In addition, it shows a nice example of a switch statement inside the Expression portion of a calculated property. Fancy.
Get-ChildItem -Path 'C:\test' |
Select-Object Name,
@{L='Size';E={
switch ($_.Length) {
# Bytes
{$_ -eq 0} {"$('{0:N2}' -f $_) bytes"; break}
{$_ -eq 1} {"$('{0:N2}' -f $_) byte"; break}
{($_ -gt 1) -and ($_ -le 1023)} {"$('{0:N2}' -f $_) bytes"; break}
# Kilobytes
{$_ -eq 1024} {"$('{0:N2}' -f ($_/1KB)) Kilobyte"; break}
{($_ -gt 1024) -and ($_ -le 1048575)} {"$('{0:N2}' -f ($_/1KB)) Kilobytes"; break}
# Megabytes
{$_ -eq 1048576} {"$('{0:N2}' -f ($_/1MB)) Megabyte"; break}
{($_ -gt 1048576) -and ($_ -le 1073741824)} {"$('{0:N2}' -f ($_/1MB)) Megabytes"; break}
# Gigabytes
{$_ -eq 1073741825} {"$('{0:N2}' -f ($_/1GB)) Gigabyte"; break}
{$_ -gt 1073741825} {"$('{0:N2}' -f ($_/1GB)) Gigabytes"; break}
default {Write-Warning -Message 'Unknown Error.'}
}
}
} | Format-Table -AutoSize
Name Size
---- ----
My-A-File.txt 1.00 byte
My-A-File2.txt 445.00 bytes
My-B-File.txt 1.00 Kilobyte
My-B-File2.txt 1.30 Kilobytes
My-C-File.txt 2.09 Megabytes
My-D-File.txt 3.59 Gigabytes
I can easily see how someone might want to remove the singular vs. plural: Kilobyte vs. Kilobytes, and just use B, KB, MB, and GB, and so…
Update: I gave this some more thought today, and I really didn’t feel like it was okay to not include a modified version that uses abbreviations (B, KB, MB, GB) instead of the full words, as in the previous example. It has removed some complexity, as well, as you can see below.
Get-ChildItem -Path 'C:\test' |
Select-Object Name,
@{L='Size';E={
switch ($_.Length) {
# Bytes
{($_ -ge 0) -and ($_ -le 1023)} {"$('{0:N2}' -f $_) B"; break}
# Kilobytes
{($_ -ge 1024) -and ($_ -le 1048575)} {"$('{0:N2}' -f ($_/1KB)) KB"; break}
# Megabytes
{($_ -ge 1048576) -and ($_ -le 1073741824)} {"$('{0:N2}' -f ($_/1MB)) MB"; break}
# Gigabytes
{$_ -ge 1073741825} {"$('{0:N2}' -f ($_/1GB)) GB"; break}
default {Write-Warning -Message 'Unknown Error.'}
}
}
} | Format-Table -AutoSize
Name Size
---- ----
My-A-File.txt 1.00 B
My-A-File2.txt 445.00 B
My-B-File.txt 1.00 KB
My-B-File2.txt 1.30 KB
My-C-File.txt 2.09 MB
My-D-File.txt 3.59 GB