Monthly Archive

Categories

Useful constants

PowerShell provides easy access to some useful constants. I often see people calculating these values rather than using the constants.

PowerShell recognises kb, mb, gb, tb and pb for kilobyte, megabyte, gigabyte, terabyte and petabyte respectively. You can use them like this:

PS> 1kb; 1mb; 1gb, 1tb, 1pb
1024
1048576
1073741824
1099511627776
1125899906842624

 

Don’t leave a space between the value and the constant.

You can use them in calculations:

PS> 7247437567256292 / 1gb
6749702.21450207
PS> 7247437567256292 / 1tb
6591.50606884968
PS> 7247437567256292 / 1pb
6.43701764536101

 

Fractional values are allowed:

PS> 27.457gb
29481729261.568

 

You can use upper case or lower case to denote the constant

PS> 27.457gb
29481729261.568
PS> 27.457GB
29481729261.568

 

Next time you need to work with megabytes or other common constants don’t calculate them use the constants in PowerShell

Comments are closed.