DEV Community

Cover image for Filter with Where-Object: Find Exactly What You Need
arnostorg
arnostorg

Posted on

Filter with Where-Object: Find Exactly What You Need

Filter with Where-Object: Find Exactly What You Need

Where-Object lets you search results. Instead of looking through everything, filter to see only what matters.

How It Works

Where-Object is a filter. You give it a condition, and it shows only items that match. Think of it like a search function—you describe what you're looking for, and PowerShell returns only those results.

The pipe (|) sends results to Where-Object, which filters them based on your criteria.

Code Examples

Filter by Exact Name

# Find files named exactly "report.txt"
Get-ChildItem | Where-Object {$_.Name -eq "report.txt"}

# $_ means "each item"
# -eq means "equals"
# Returns only matching files
Enter fullscreen mode Exit fullscreen mode

Filter by File Size

# Show files larger than 1MB
Get-ChildItem -File | Where-Object {$_.Length -gt 1MB}

# Show files smaller than 100KB
Get-ChildItem -File | Where-Object {$_.Length -lt 100KB}

# -gt means "greater than"
# -lt means "less than"
Enter fullscreen mode Exit fullscreen mode

Filter by Date

# Show files modified in last 7 days
Get-ChildItem | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-7)}

# Show files older than 30 days
Get-ChildItem | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)}
Enter fullscreen mode Exit fullscreen mode

Combine Multiple Filters

# Show .log files bigger than 1MB modified in last 30 days
Get-ChildItem | Where-Object {$_.Name -like "*.log" -and $_.Length -gt 1MB -and $_.LastWriteTime -gt (Get-Date).AddDays(-30)}

# -and means ALL conditions must be true
# -like uses pattern matching
Enter fullscreen mode Exit fullscreen mode

Most Used Options

  • -eq - Equals (exact match)
  • -like - Pattern match (use * wildcards)
  • -gt - Greater than (larger)
  • -lt - Less than (smaller)
  • -and - Both conditions must be true
  • -or - Either condition can be true

The Trick: Power Usage

Combine Where-Object with other commands to clean up your system:

# Find and delete .tmp files older than 30 days
Get-ChildItem *.tmp | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | Remove-Item

# But be careful! Test first with -WhatIf:
Get-ChildItem *.tmp | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | Remove-Item -WhatIf
# -WhatIf shows what WOULD happen without actually deleting
Enter fullscreen mode Exit fullscreen mode

Learn It Through Practice

Stop reading and start practicing right now:

👉 Practice on your browser

The interactive environment lets you type these commands and see real results immediately.

Next in PowerShell for Beginners

This is part of the PowerShell for Beginners series:

  1. Getting Started - Your first commands
  2. Command Discovery - Find what exists
  3. Getting Help - Understand commands
  4. Working with Files - Copy, move, delete
  5. Filtering Data - Where-Object and Select-Object
  6. Pipelines - Chain commands together

Related Resources

Summary

You now understand:

  • How this command works
  • The most common ways to use it
  • One powerful trick to level up
  • Where to practice hands-on

Practice these examples until they feel natural. Then tackle the next command in the series.


Ready to practice? Head to the interactive environment and try these commands yourself. That's how it sticks!

What PowerShell commands confuse you? Drop it in the comments!

Top comments (0)