Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


April 2008

PowerShell 101, Lesson 3

How to use PowerShell's operators and wildcards
RSS
Subscribe to Windows IT Pro | See More Systems Administration Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!
SideBar    How to Find Out a Cmdlet's Properties

Executive Summary:

Windows PowerShell offers many different types of operators. For example, its comparison and logical operators let you create conditions that PowerShell evaluates to determine whether it should take specific actions. And its arithmetic operators let you not only perform mathematical calculations but also concatenate strings.


In PowerShell, you can connect cmdlets together to create a pipeline and use the Where-Object cmdlet to filter objects passed down that pipeline. For example, in the following statement, objects from the Get-ChildItem (referenced by the dir alias) cmdlet are piped to a Where-Object cmdlet (referenced by the where alias), which filters out all items in C:\Windows, except those larger than 500,000 bytes:

 dir c:\windows |
  where {$_.length -gt 500000}

Notice that the Where-Object cmdlet includes an expression that’s enclosed in braces ({ }). The expression states that the current value of the Length property must be greater than 500,000. The value of the Length property is retrieved by using $_.length. The $_ symbol references the current object in the pipeline, and .length retrieves the value of the Length property. The expression then uses the -gt (greater than) operator to compare the Length property value to the value of 500,000.

As with any language, PowerShell provides a set of operators that let you create expressions you can incorporate into your statements. An expression is a block of code that PowerShell evaluates; the result of that evaluation determines what action to take. For example, in the preceding statement, PowerShell determines whether the Where-Object expression is true or false. When the expression evaluates to true—that is, the current object’s Length property value is greater than 500,000—that object is passed down the pipeline and displayed in the output. If the expression evaluates to false—that is, the current object’s Length property value isn’t greater than 500,000—the object is discarded and not displayed in the output.

PowerShell includes a variety of operators that you can use in your expressions. This lesson describes many of those operators and provides examples of how to use them. In addition, the Web-exclusive sidebar “How to Find Out a Cmdlet’s Properties,” www.windowsitpro.com, InstantDoc ID 98175, discusses how to find property names, such as those used in the Where-Object expressions in the examples provided.

Comparison Operators
As the name suggests, comparison operators compare values. When an expression contains a comparison operator, PowerShell compares the value to the left of the operator with the value to the right of it. You saw this idea in the preceding example, in which the Length property value is compared to 500,000. PowerShell provides many comparison operators, as Table 1, shows. Let’s examine some of those operators to see how they work.

The following statement does the opposite of the preceding example— it returns items whose lengths are smaller than 500,000 bytes:

 dir c:\windows |
  where {$_.length -lt 500000}

As you can see, the only difference between the two statements is the comparison operator. This statement uses the -lt (less than) operator rather than the -gt operator.

Other comparison operators follow the same logic. The following statement uses the -eq (equal to) operator to compare the Responding property value to the string true in order to retrieve a list of responding processes:

 get-process |
  where {$_.responding -eq “true”}

For the Where-Object expression to evaluate to true, the Responding property value must equal true. As a result, only responding processes are returned, as Figure 1 shows.

By default, all comparison operators perform case-insensitive comparisons. If you want to be more precise in your code, you can add the letter i to a comparison operator (e.g., -ieq) to explicitly specify a case-insensitive comparison. However, because this is the default behavior, adding the i isn’t necessary.

You can make any comparison casesensitive by adding the letter c to the comparison operator (e.g., -ceq). For example, the statement

 “True” -eq “true”

evaluates to true because it ignores case, whereas the statement

 “True” -ceq “true”

evaluates to false because it takes case into account.

I realize that these are very basic examples, but when working in a Windows environment, case often isn’t a concern because filenames, process names, and other item names are case-insensitive. But as you become more familiar with PowerShell and learn how to retrieve other types of lists in which case-sensitivity is important, you’ll find being able to make an operator casesensitive useful.

Another useful PowerShell feature is wildcards. For example, if you don’t know the exact name of an item when creating an expression to compare values, you can use wildcards in the compared value (the value after the operator). Table 2 describes the wildcards that PowerShell supports.

Continued on page 2

   Previous  [1]  2  3  Next 


Learning Path To read the previous "PowerShell 101" lessons, go to
"PowerShell 101, Lesson 1"

"PowerShell 101, Lesson 2"


For more information about how to use the Get-Member cmdlet
"What Can I Do With Windows PowerShell? Using the Get-Member Cmdlet"

"Get-Member"


For more information about using PowerShell operators
"Windows PowerShell Constructs"

"What Can I Do With Windows PowerShell? Using the Where-Object Cmdlet"


If you're beyond the basics, check out
"PowerShell Queries for Failed Services on Remote Machines"

"PowerShell One-Liners for Accessing WMI"


Top Viewed ArticlesView all articles
What You Need to Know About Microsoft's x64 Server Product Plans

What do Longhorn Server, Windows Compute Cluster Server, and Windows Vista have in common? The x64 platform. ...

WinInfo Short Takes: 4th of July Special Edition

An often irreverent look at some of the week's other news, including a shortened work week thanks to the 4th of July, expensive Windows 7 pricing, Bing's modest monthly gains, IE 8 heading to work, Steve Jobs back at Apple, and so much more ...

How can I stop and start services from the command line?

...


Related Articles PowerShell Script Lets You Check Patches' Status

How to Get Information About Installed Applications Without Using WMI

PowerShell Pointers

Quest Freeware Product Puts a Familiar, GUI Face on PowerShell

Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events WinConnections and Microsoft® Exchange Connections

PowerShell 201 - eLearning Series with Paul Robichaux

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 Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format

Test Drive IT Solutions and Get Free Music Downloads
Solve your toughest IT problems with these free downloads and receive 5 free music downloads!


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home asp.netPRO Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement | Reprints and Licensing