PowerShell Ezine, Logon Scripts

Windows PowerShell | Where {$_.property = statement}

Windows PowerShell | Where {$_.property = statement}

I started my computing career in 1982 with a spreadsheet called SuperCalc.  The first command that I mastered was the 'If' statement, it was wonderful for filtering data.  Now, although PowerShell also supports the 'If' statement, I find PowerShell's 'Where' construction much more flexible and versatile for filtering the output.  Let us learn more about 'Where' by examining the following practical examples.

PowerShell Topics for the Where statement

Instructions to Run PowerShell code:PowerShell copy and paste

Method 1 (Quick)

  • Launch PowerShell
  • Copy the code into memory
    (For instance, from Example 1a)
  • Right-click on the PowerShell symbolPowerShell Scripts
  • Edit --> Paste
  • Press enter to execute the code
  • See screenshot to the right

 

Method 2 (Best)

  • Prepare to run cmdlets with this PowerShell command:
    set-ExecutionPolicy RemoteSigned
  • Copy the code below into a text file.
  • Save the file with a .ps1 extension, for example: network.ps1
  • In PowerShell, navigate to where you saved network.ps1
  • Issue this command:
    .\network 
    (dot backslash filename)

'Where' Examples Which Filter Lists of Files

A good time to call for a 'Where' statement is when you need to filter a list of files.    What we are going to do is get a list of files with (get-childitem), and then pipe the output into a 'Where' clause, which filters according to the condition: file extension equals .exe.

Example 1a Where clause to find executable files

 

clear-Host
# PowerShell script to list the exe files C:\program Files
get-childitem "C:\Program Files" -recurse | where {$_.extension -eq ".exe"}

Note 1: clear-Host is an optional command to clear the screen.

Note 2: # (hash) means: 'Here follows a comment'

Example 1b Where replaced with '?'

# PowerShell script to list the exe files C:\Program Files

$GuyDir = "C:\Program Files"
$FilesExe = gci $GuyDir -recurse
$List = $FilesExe | ? {$_.extension -eq ".exe"}
$List | sort-object -unique | format-table name

Learning Points for PowerShell's Where

Note 1:  Many PowerShell script writers prefer the single character, '?' instead of 'Where', however I prefer to stick with the full word 'Where'.

Note 2:  PowerShell has more aliases for common commands, see gci in Example 1b.  In Example 1b we could also use ft instead of format-table; mostly I use plain sort rather than sort-object.

Note 3:  Always remember to introduce the where statement with a pipe (|), hence, | where{$_......

Note 4:  Observe how sorting and formatting can improve the output.  To see what I mean compare Example 1a with Example 1b.

Challenge 1:  Change ".exe" to ".dll"

Challenge 2:  Change: where {$_.extension -eq ".exe"} to where {$_.name -eq "ReadMe"}

Challenge 3:  Try changing the location C:\program files to a folder of your choice.  If you accept this challenge, consider changing the file extension.


If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


'Where' Examples Which List WMI Objects Containing "Network"

Another situation where you can employ a 'Where' statement is when you need to filter objects.  To take a network example, our goal is to display TCP/IP properties, but our problem is 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.

My idea behind giving you three examples of achieving the same goal is to give you perspective, alternatives, and to illustrate that there is no one 'right' way of coding PowerShell.

Example 2a

 

clear-Host
get-wmiobject -list | where {$_.name -match "Network"}

Example 2b

 

clear-Host
gwmi -list | ? {$_.name -match "Network"}

Example 2c

 

clear-Host
$objNetwork = get-wmiobject -list | where {$_.name -match "Network"}
$objNetwork |Format-Table name

Learning Points for PowerShell's Where

Note 1:  Always remember to introduce the where statement with a pipe (|), hence, | where{$_......

Note 2:  Example 2b shows us that you can use a question mark (?) to replace Where.  This example also employs the alias gwmi for get-wmiobject.

Note 3:  Most 'Where' statements employ the '{$_.xyz' construction.  What the first half of this statement does is say: 'Take the xyz from the current input'. 

The other half of the statement is concerned with an evaluation, which achieves the filtering that we desire.  In these examples I use -match, but it could be -eq (equals), -like, or any of PowerShell's comparison operators.

Note 4: One lesson that I am for ever re-learning with PowerShell is that every word or symbol is packed with meaning.

See updates on 'Where' and also more on PowerShell

ˆ

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.