Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 



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!

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 


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?

...


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

Related Events Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

PowerShell 201 - eLearning Series with Paul Robichaux

Cloud Computing Forum: Integrating Software, Server and Storage as a Service into Your Enterprise IT Delivery Model

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.


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 Windows Dev Pro IT Job Hound 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