The FileInfo object is great, with lots of useful information. But there is one piece of information I miss, and that is the SizeOnDisk information that we are used to from Windows Explorer. Well, let’s do something about that, shall we?
There is actually two parts to getting this “problem” solved. To calculate the size a file takes on disk, we need to know the cluster size of the disk volume. This information are available in the Win32_Volume class in WMI. Ok, great, but since this is a disk/volume-realted piece of information, I feel it doesn’t make sense to add this to FileInfo. But there is another object where it DO belong; the PSDriveInfo object.
So there is in fact two object types we need to extend. First we need to add the block size to PSDriveInfo, and then update FileInfo to use this info to calculate the size on disk value.
It’s really not much to it, but I have done the heavy lifting for you. Just copy/paste the following two functions to your PowerShell profile, and you will always have BlockSize and SizeOnDisk available when working with drives and files. (Remember to also run the functions :))