PowerShell 3.0 Where Statement

PowerShell 3.0 Where Clause Simplified

PowerShell 3.0 simplifies the ‘Where’ clause by not requiring either the {curly} brackets or the placeholder $_.xyz.  The benefit is that it makes easier for beginners to write PowerShell scripts.

PowerShell 3.0 Where Examples of the New Syntax

 ♣

Example 1: PowerShell v 3.0 Where Example

Scenario: You want to list the dynamic link library files in the Windows System folder.

Example 1a: New Style PowerShell 3.0's Where Syntax

Get-Childitem C:\Windows\System32 | Where extension -eq .dll

Note 1: What’s new in PowerShell 3.0 is the simpler syntax
Where extension -eq .dll

Example 1b: Old Style PowerShell 2.0's Where Syntax

In PowerShell 2.0 the above command would require three extra syntax elements: {Curly brackets}, the $_. variable and speech marks around the value:

Get-Childitem C:\Windows\System32 | Where {$_.extension -eq .dll}

Note 2: I feel a bit of a Philistine because I prefer the full syntax as shown in example 1b.  The shorter method is all very well, but I have found it can cause me confusion and introduce errors.

Example 2: PowerShell 3.0 Where Lists .exe Files

# PowerShell 3.0 ‘Where’ filter for listing .exe in system folder
Clear-Host
Get-ChildItem $Env:WinDir | Where extension -eq .exe

Note 3: $Env:WinDir is an environmental variable, which just get’s your Windows folder even if it’s on the D:\ and even if you call it WindowsOld.

Challenge: Check the speed of the two techniqes
You can wrap the whole command in Measure-Command and thus compare the speed of the two v2.0 'Where' with the new v3.0 'Where.

Clear-Host
Measure-Command {
Get-ChildItem $Env:WinDir | Where extension -eq .exe
}
Measure-Command {
Get-ChildItem $Env:WinDir | Where-Object {$_.Extension -eq '.exe'}
}

Suggestion: My advice is to run the command 3 or 4 times to make sure caching is causing inconsistent results.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Example 3: Where Can Still Be Replaced With ‘?’

You can simplify the filter further by substituting the question mark for the command ‘Where’, for example: ? extension -eq .exe  (Instead of: Where extension -eq .exe)  The use of the question mark is the same as in PowerShell 2.0, as is the use of the alias gci for Get-ChildItem.

# Windows PowerShell Where script to list exe files C:\Program Files
$FilesExe = gci "C:\Program Files" -recurse
$List = $FilesExe | ? extension -eq .exe
$List | Sort-Object -unique | Format-Table Name, Directory -auto

Learning Points for PowerShell’s Where Statement

Note 4: Most people who use PowerShell prefer to type the question mark character ‘?’ instead of ‘Where’.  However, I prefer to stick with the full word ‘Where’ because it makes the script easier to read especially with complex statements.

Note 5: If you like these abbreviations, PowerShell has numerous aliases for common commands, for example in the above script note, gci for Get-ChildItem.  Furthermore, we could employ ‘ft’ instead of Format-Table.  Even I usually omit the Object from Sort-Object and Where-Object.

Note 6: What I often forget is to introduce the where statement with a pipe, hence,
….. $FilesExe | ? ……  Please learn from my mistake!

Example 4: PowerShell ‘Where‘ Filters WMI Objects

Another situation that benefits from a ‘Where’ statement is when we research PowerShell’s objects.  To take a network example, imagine that our goal is to display the TCP/IP properties, but we have a problem – what is the WMI object called?  Let us begin our quest by researching WMI objects.  We already know that we can use, WmiObject -List, but let us refine our search by adding a where statement.

Where Example 4a

# Windows PowerShell where clause in WMI script
Get-WmiObject -List | Where name -Match Network

Where Example 4b

Once again, we can substitute the question mark for ‘Where’; I also added FT to select just the name.

# PowerShell ? (where) filter to list Network WMI objects
Clear-Host
Get-WmiObject -List | ? name -Match Network | FT name

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Researching PowerShell 3.0’s Where-Object

PowerShell’s where statement is so common that we tend to forget the ‘?’ and even ‘Where’ are aliases for the Where-Object cmdlet.  The benefit of this realization is that we can call for Get-Help.

Get-Help Where-Object

Both the new 3.0 'Where' and the old v 2.0 version are aliases for the cmdlet Where-Object.

Clear-Host
Get-Help Where-Object -full
# Plain ‘help where’ also works.

Help reminds us that Where-Object does its filtering by examining a {Scriptblock}.  I have never seen ‘where’ in isolation, it always receives its input via a pipe (|).

One more point to note, PowerShell uses its own comparison objects such as -gt or -le, and not > or =<.

Where-Object Extra Parameters in PowerShell 3.0

Clear-Host
Get-Command Where-Object | Select -ExpandProperty ParameterSets |
Format-Table Name

Note 7: One of the interesting new parameter in version 3.0 is ‘In’.  For example try:
2 -In 1..10

PowerShell 3.0 Where-Object with Get-ChildItem

I am assuming that you can research a cmdlet’s properties by appending gm (Get-Member).  For the first experiment we need the ‘Extension’ property, and later we need LastAccessTime.

Experiment 1: We want a list of the executable (.exe) files in the Windows folder.

Method: We can use PowerShell 3.0’s simple ‘Where’ clause.

# PowerShell 3.0 Get-Childitem Where
Get-Childitem C:\Windows | Where Extension -eq .exe

Guy Recommends:  SolarWinds’ Free Bulk Import ToolFree Download Solarwinds Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD’s attributes, click and import the users.

Optionally, you can provide the name of the OU where the new accounts will be born. Download your FREE bulk import tool.

If you need more comprehensive application analysis software,
Download a free trial of SAM (Server & Application Monitor)

Problem with PowerShell 3.0’s Where Clause

Following the previous example you would think that | Where LastTime.Year -gt 2012 would provide a list of files that were accessed after 1/1/2013, but it doesn’t.

# PowerShell 3.0 Where Problem
Get-Childitem C:\Windows\System | Where LastAccessTime.Year -gt 2012

Although the above example completes without error it does not display any files.  Just in case there are no files, change -gt to -lt, still no files appear.

Solution:  Revert to the PowerShell 2.0 Where syntax.  Remember the full syntax: Curly brackets}, $_. variable, and ‘single speech mark’.

# PowerShell 3.0 Where Clause Problem
Get-Childitem C:\Windows\System | Where {$_.LastAccessTime.Year -gt ‘2012’ }

Other PowerShell Cmdlets Containing ‘Object’

Clear-Host
Get-Command -Noun object

Summary of The New PowerShell 3.0 ‘Where’ Clause

PowerShell 3.0 simplifies the syntax of the ‘Where’ statement.  We no longer need either the {curly} brackets or the placeholder $_.xyz.  As a result it’s easier for beginners to learn PowerShell, and it’s easier for everyone to understand ‘Where’ clauses.

See more examples of PowerShell’s ‘Where’ clause in action:

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials

PowerShell Tutorials  • Methods  • Cmdlets  • PS Snapin  • Profile.ps1  • Exchange 2007

Command & Expression Mode  • PowerShell pipeline (|)  • PowerShell ‘where‘  • PowerShell ‘Sort’

Windows PowerShell Modules  • Import-Module  • PowerShell Module Directory 

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links.