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.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

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

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

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