Saturday, April 28, 2007

Counting in GB/MB/KB In PowerShell

One of the cooler features within PowerShell is the ability to calculate using 'real'  kb, mb, gb. By 'real', remember that 1 kilobyte (kb) should be 1024 and thus 10kb is 10,240 (and not as disk manufacturers like to use, 10,000). The same holds true for megabytes (1mb = 1,048,567 vs 1,000,000),   and gigabytes (1gb = 1,073,741,824 vs 1,000,000,000). 

The reason all this matters is that when viewing object sizes you can get figures in either raw bytes, or in kb/mb/gb. PowerShell helps by defining a special set of constants to represent kb, mb, and gb.

You can demonstrate this in PowerShell as follows:

PSH [D:\foo]: 1kb
1024
PSH [D:\foo]: 1mb
1048576
PSH [D:\foo]: 1gb
1073741824

This is pretty useful when calculating  file sizes, for example:

PSH [E:\vpc vms\LonghornB3]: (ls bigupload.zip).length.tostring("##,###.#")
2,795,251,968
PSH [E:\vpc vms\LonghornB3]: ((ls bigupload.zip).length/1mb).tostring("##,###.#")
2,665.8
PSH [E:\vpc vms\LonghornB3]: ((ls *.zip).length/1gb).tostring("##,###.#")
2.6

One unfortunate omission, however, in PowerShell V1.0 is that the dev team did not do a land grab for more constants (i.e. tb=terabyte, pb=petabyte and eb=exebyte). Today, Hitachi is shipping a 1tb disk - thus within the life of Vista/Longhorn, TB will become a not uncommon size (of course SANs today measure in the the TBs already). The Petabyte/Exebyte ranges may be a bit of a ways off being commonplace, but why not just reserve them now? I hope this is something that gets added to V2.0.

Technorati tags: ,

No comments: