May 20, 2024

How to use the Where Statement in PowerShell?

In PowerShell, the “where” keyword is used to filter the results of a command or expression based on specified criteria. It is often used in conjunction with the “select-object” cmdlet to filter the objects that are returned by a command.

Another Powerful feature you can use in PowerShell is the Where Statement.
With the Where statement, you can search the PowerShell properties for your item.

In this example, we will use the following file structure for showing how and when to use the Where statement.

Now it’s time to play with the where statement.
First, we need to find out which properties we can filter with where. To find out the properties you can do the “| Get-Member” .

Let’s play with the name property first. Filter all filenames on the letter o.
Show the files with a “o” in the name:

Filter all files created with Microsoft Word. This is possible with the extension property filter. In this example, we use the equals (-eq) conditional operator.

If you not know which property a PowerShell object have you can always use the happy guessing factor. {$_} without a property behind it.

With a more advanced Where query you can get files that are created before a date using PowerShell.
In the first query, show all files which are edited after Yesterday. Greater than (-gt) yesterday. Or Lower than (-lt) Yesterday. Created the files today.. so nothing is created before Yesterday.

Below are all queries we discussed. So you can practice it yourself.

If you have other questions about the where statement? Ask me anything below.

Nick Wijnands

Hello, my name is Nick. I have a passion for Computers and i love to share my knowledge and write about it. If you have any questions, dont mind to ask them. Happy Reading.

View all posts by Nick Wijnands →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.