Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

April 29, 2008 12:00 AM

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

Windows IT Pro
InstantDoc ID #98988
Rating: (0)

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.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.