A Common Misconception About PowerShell's kb, mb, and gb Operators

People sometimes refer to PowerShell's kb, mb, and gb operators as constants. However, they aren't constants; they're operators.

Alex Angelopoulos

April 28, 2008

1 Min Read
ITPro Today logo in a gray background | ITPro Today

People occasionally refer to PowerShell's kb, mb, and gb operators as constants. However, they aren't constants; they're operators. As of this writing, even some Microsoft documentation makes this mistake (http://www.microsoft.com/technet/scriptcenter/resources/pstips/jun07/pstip0608.mspx).

PowerShell does have values called constants. (Although they aren't precisely what most scripting and programming languages would call constants.) However, they behave nothing at all like the kb, mb, and gb operators. The difference can best be seen through an example.

In PowerShell, you can use the New-Variable cmdlet to create a constant. For example, the statement

New-Variable -Name kb -Value 1024 -Option Constant

creates a constant named kb, which has the value of 1024. You can then use this constant to find out how many bytes there are in 32 binary kilobytes (32KiB) by multiplying it by 32:

32 * $kb

As this example shows, when using a constant, you have to precede its name with a dollar sign ($) so that PowerShell recognizes it as a named value. The arithmetic * operator is used to perform the multiplication.

In contrast, when PowerShell comes across the kb operator in a statement such as

32kb

kb is parsed into a separate token and taken to mean "multiply the preceding value by 1024". Because kb operates on a single, preceding value, it's technically known as a unary postfix operator.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like