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 scripts, and for everyone to understand 'Where' clauses.
Scenario: You want to list the dynamic link library
files in the Windows System folder.
Example 1 New Style PowerShell 3.0 Where
Get-Childitem C:\Windows\System | Where extension -eq .dll
Note 1: What's new in PowerShell 3.0 is the simpler Where
extension -eq .dll
In PowerShell 2.0 the above command would require three extra syntax elements:
{Curly brackets}, the $_. variable and speech marks around the 'value': where
{$_.extension -eq '.dll'}
Example 2 PowerShell 3.0 Where Lists System .exe Files
# PowerShell 3.0 'Where' filter for listing .exe in system folder
Clear-Host Get-ChildItem $Env:WinDir | Where extension -eq
.exe
Note 2: $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.
Guy
Recommends: 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.
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 3:
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
4: 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 5: What I often forget is to introduce the where statement with a pipe, hence, .....
$FilesExe | ? ...... Please learn from my mistake!
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'
# PowerShell ? (where) filter to list Network WMI objects
Clear-Host Get-WmiObject -List | ? name -Match Network | FT name
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
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.
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
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 =<.
Note 6: One of the intesting 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 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.
Following the above example you would think that | Where LastTime.Year
-gt 2010 would provide a list of files that were accessed after 1/1/2011,
but it doesn't.
# PowerShell 3.0 Where Problem Get-Childitem C:\Windows\System | Where
LastAccessTime.Year -gt 2010
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 '2010' }
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:
Windows Management Instrumentation (WMI) is
most useful for PowerShell scripting.
SolarWinds
have produced this
Free WMI Monitor to take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.