New Where-Syntax

by Mar 31, 2016

In PowerShell 4.0 and better, there is a new alternative to the Where-Object cmdlet:

$all = Get-Service 

$all | Where-Object { $_.Status -eq 'Running' }

$all.Where{ $_.Status -eq 'Running' }

The Where() method is special and does not need the outer parenthesis. It simply needs a script block to iterate. There is also a Foreach() method that works like ForEach-Object. The main difference between the classic cmdlets and the new methods is the way they are executed: the methods do not use a pipeline. So the overhead should be less.

Twitter This Tip! ReTweet this Tip!