April 29, 2008

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


RSS
View this exclusive article with VIP access -- click here to join |
See More Scripting Articles Here | Reprints | Or sign up for our VIP Monthly Pass!
Main Article    Windows PowerShell Range Operator Tricks

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. . . .

Top Viewed ArticlesView all articles
CES 2009: Ballmer Announces Windows 7, Windows Live, Live Search Milestones

During his first-ever Consumer Electronics Show (CES) 2009 keynote address last night in Las Vegas, Microsoft CEO Steve Ballmer announced the pending public availability of a feature-complete Windows 7, the final version of Windows Live Essentials, and ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

Where is Microsoft NetMeeting in Windows XP?

...


Related Events PowerShell 201 - eLearning Series with Paul Robichaux

PowerShell 101 - eLearning Series

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


SQL Server Magazine Office & SharePoint Pro Windows Dev Pro ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing