Where-Object Versus Where Method

Adam Bertram explains the difference between the where-object and the where method in Powershell:

The Where-Object command is a sort of generic filtering command. Use this command to filter any kind of object in PowerShell. The Where-Object command has a FilterScript parameter, which is a scriptblock that allows the user to place code in it. If this scriptblock contains code that returns anything but $false, $null, or an empty string, it will allow whichever object the user passes to it.

For example, let’s say I’ve got a folder full of files. I’d like to see only text files and only those text files modified today. To make this happen, I can use the provider-specific filter with the Get-ChildItem command and also the Where-Object command.

Read on to see how that compares to the where method.  Given the latter’s limitations, I’ll probably stick to where-object anytime performance is not critical.

Related Posts

Publishing DACPACs With Powershell

Richie Lee has a new set of functions for dbatools: There are 3 modules included. I’m going to write a bigger piece about these functions elsewhere, but for now here is a link to the documentation – Export-DbaDacpac New-DbaPublishProfile Publish-DbaDacpac Read on for more information and a link to the source code if you’re interested […]

Read More

String Formatting With Powershell

Thomas Raynor provides a quick tip for string formatting in Powershell: And, for some reason, instead of the default output which is formatted like a table, I want output presented like this. 1 2 3 .ps1     file extension: 11 .xlsx    file extension: 3 .dll     file extension: 1 This is a silly example, but notice that even […]

Read More