PowerShell for Windows Admins

May 26 2019   2:01PM GMT

Useful constants

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell

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

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: