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

You implement wildcards through the use of the -like and -notlike comparison operators. (Note that -like and -notlike as well as -match, -notmatch, and -replace are sometimes referred to as pattern-matching operators.) For instance, suppose you want to find all Google-related processes on a computer. You can use the -like operator to return all processes created by companies whose name includes the string google:

 get-process |
  where {$_.company -like “*google*”}

The asterisk wildcard matches zero or more characters, so you’ll receive accurate results no matter whether the company name is stored in Windows as Google, Google Inc., or another variation. Figure 2 shows the results from this statement. If you were to use the -notlike operator instead of the -like operator, all non-Google processes would be returned.

In addition to wildcards, PowerShell supports regular expressions, which are based on the Microsoft .NET Framework regular expression classes. You implement regular expressions through the use of the -match and -notmatch operators. PowerShell’s support for regular expressions is quite extensive— as extensive as you would find in any .NET language. For this reason, a discussion about them is beyond the scope of this lesson. For information about them, see PowerShell’s about_regular_expression and about_comparison_ operators Help files.

Logical Operators
So far, I’ve discussed how to use comparison operators in expressions. When you use one of these operators, you create a condition that’s evaluated to determine whether to take a specific action. However, in some cases, you might want to create expressions that include multiple conditions. In other words, you might want to perform more than one comparison to determine whether to take that action.

To perform multiple comparisons in a single expression, you must use logical operators to link conditions together. Logical operators, which are described in Table 3, specify what logic to use when evaluating multiple conditions.

Let’s take a look at an example to illustrate how logical operators work. The following statement uses the Get- Process cmdlet to retrieve a list of running processes:

 Get-Process |
  where {($_.handles -gt 500) `
  -and ($_.pm -ne 0)} 

There are two conditions, each of which is enclosed in parentheses. The first condition ($_.handles -gt 500) specifies that the number of handles must be greater than 500 for a given process. The second condition ($_.pm -ne 0) specifies that the paged memory size must not equal 0. The -and logical operator connects these two conditions. As a result, both conditions must evaluate to true for the entire expression (enclosed in braces) to evaluate to true. Only those processes that meet both of these conditions are returned, as Figure 3 shows.

Now let’s take a look at the -or operator. The following statement is the same as the preceding example except that it uses -or instead of -and:

 get-process |
  where {($_.handles -gt 500) `
-or ($_.pm -ne 0)} 

In this case, at least one of the conditions must evaluate to true for a process to be included. In other words, the process must have a handle count greater than 500 or the paged memory size must not equal 0 or both. As a result, many more processes are returned, as Figure 4 shows.

You can use the -not logical operator to indicate that a specified condition must not be true. For example, the following statement specifies that the handle count must be greater than 100 and the company name must not be Microsoft Corporation:

 get-process |
  where {($_.handles -gt 100) `
  -and -not ($_.company -eq `
  “Microsoft Corporation”)}

This statement returns all non-Microsoft processes, as Figure 5 shows.

Continued on page 3

   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
Microsoft, News Corp. Discuss Locking Out Google

Microsoft and Rupert Murdoch's News Corp. recently discussed an alliance that would counter Google's fledgling online news service. ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

Command Prompt Tricks

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


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

Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

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


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
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement