PowerShell -Filter vs. Where-Object – what is the main difference?

Well – timing. For instance if we would like to ask AD for certain user accounts,  -Filter will process the data on the remote computer (domain controller) and will send us only the filtered results, whilst Where-Object (?) will sends us all the results, and “filtering” takes places on our end. Here is a small example of the speed differences:

measure-command {Get-remotemailbox -Filter {RecipientTypeDetails -eq "RemoteRoomMailbox" -and name -like "*people*"}}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 756
Ticks             : 7564893
TotalDays         : 8,75566319444444E-06
TotalHours        : 0,000210135916666667
TotalMinutes      : 0,012608155
TotalSeconds      : 0,7564893
TotalMilliseconds : 756,4893

measure-command {Get-remotemailbox | ? {$_.RecipientTypeDetails -eq "RemoteRoomMailbox" -and $_.name -like "*people*"}}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 18
Milliseconds      : 415
Ticks             : 184153984
TotalDays         : 0,000213141185185185
TotalHours        : 0,00511538844444444
TotalMinutes      : 0,306923306666667
TotalSeconds      : 18,4153984
TotalMilliseconds : 18415,3984

Filter was 24 times faster!

2 thoughts on “PowerShell -Filter vs. Where-Object – what is the main difference?

  1. The only downside being that there is no unified syntax for filtering among the different cmdlets for different application coming from different teams within microsoft. Some use the intuitive powershell-y syntax, and some are almost literal sql statements.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s