SID-500

Home » PowerShell » PowerShell: Filtering Objects vs Where-Object

PowerShell: Filtering Objects vs Where-Object

In this article I would like to compare the filter parameter with where-object. The teaching says, that filtering is more efficient.  In the case of where-object, all objects must be retrieved first and then filtering is performed. This takes time. If filtered first, only the filtered objects are retrieved. In this article we’ll look at this using Measure-Command.

Get-Childitem and Where-Object

Measure-Command {Get-Childitem -Path C:\ -File -Recurse -ErrorAction SilentlyContinue | Where-Object {$_.Extension -eq ".txt"}}

1.PNG

37 seconds is not bad. I have a SSD.

Get-Childitem with Filter

Measure-Command {Get-Childitem -Path C:\ -File -Recurse -ErrorAction SilentlyContinue -Filter *.txt}

2.PNG

8 seconds is much faster.

Conclusion

Filtering is much faster. But not all commands offer the possibility to filter. In many of my posts I use where-object instead of the filter parameter. Why? I’m used to it. I think i have to change. 😉


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Categories

Who is the blogger?

My name is Patrick Grünauer (pewa2303). I am from Austria. On sid-500 I write about Windows, Cisco and IT-Security in English and German. Have fun while reading!

Patrick Gruenauer
Follow SID-500 on WordPress.com