Formatting byte size numbers with PowerShell

On This Page

For this week, I’m going to share another classic for me, my Format-Bytes function. This is one of those helper functions I’ve got that I scoff at because it is so simple, but I miss it whenever I need it.

The problem

Ever used PowerShell to look at the size of a file? Say an Exchange ISO file?

Can you glance at that number and tell me how big that is? Since it is an Exchange installation ISO file, I can assume that the 5 at the front means that it is either 500MB or 5GB. Though if I spent some more time thinking about that, I’d probably guess 5GB.

Adding commas

If we put commas in that number, it would make it easier to read:

"{0:n0}" -f 5161132032
5,161,132,032

But even that could be made easier to understand.

A solution

With PowerShell, we can take a number representing a size in bytes, divide it by the right number of 1000s, and add the proper suffix rather easily!

Determining the size

Did you know that you can create a number that is 1024 by using 1KB? This means that we can make easier to understand comparisons to find out how big a number really is:

Formatting the number

But how would we go about doing so? This is where we can use the format operator in PowerShell. The official docs are a little sparse and reference .NET framework stuff which isn’t all that useful to most PowerShell users, so I’d recommend checking out the SS64 version which explains the :n syntax that I’ll be using.

The short version is that using {0:n} will format a number and you can specify decimal places by placing a number after like: {0:n2}.

Combining them

And we can confirm that by looking at the properties of the file itself:

So it is a bit smaller than the 5GB I originally estimated.

To function or not to function

If you’ve read any of my other posts, you know that there is a good chance I’ll be functionizing this. Well, you are right! But I want to show another piece of code as well:

Preamble

So with this function, you probably noticed all the if/elseif statements from earlier. Meaning that if I wanted to add petabytes, exabytes, zettabytes, or yottabytes I’d have to add an elseif statement for each. Which is way too difficult.

Instead I would suggest starting with an array of the sizes, and then iterating through that:

While this is harder to read, it makes it a cinch to add in additional sizes. Though, as I just discovered, PowerShell only supports up to petabytes. So I’ve limited the above script to what is actually supported.

But keep in mind that this is simple a string format of the number. If you took that number and tried any comparison or arithmetic operators, they would treat it as a string.

Conclusion

I hope you find this useful! But I also hope it inspires you to build even simple tools such as this one. Its quite amazing how many times I’ve just wanted to see a formatted version of a long byte number.

If you’ve got any feedback you can comment here, tweet at me, open an issue in my Utilities repo, or shoot me an email. Whatever you prefer.