PowerShell Get-ChildItem Filter Examples

If you’ve ever needed to list, search, or filter files and folders in Windows PowerShell, you’ve probably used the Get-ChildItem cmdlet — often abbreviated as gci.

This PowerShell command is useful for navigating the file system, filtering files by extension, size, and date, and even using wildcards or regular expressions.

In this tutorial, we’ll explore real-world PowerShell Get-ChildItem filter examples, from basic to advanced, so you can efficiently manage files on your system.

What is Get-ChildItem PowerShell Cmdlet?

The Get-ChildItem cmdlet retrieves the items (files and folders) in a specified location. It’s like a more powerful version of the dir or ls command in Command Prompt or Linux.

Syntax:

Get-ChildItem [-Path] <String[]> [-Filter <String>] [-Recurse] [-File] [-Directory]

Example:

Get-ChildItem -Path "C:\Users\Public\Documents"

This lists all files and folders in the specified directory.

Understanding the -Filter Parameter

The -Filter parameter in the Get-ChildItem cmdlet allows you to narrow down the results that PowerShell retrieves. It’s faster than using Where-Object because the filtering happens at the provider level (e.g., the file system), not after all results are loaded.

Example:

Get-ChildItem -Path "C:\Logs" -Filter "*.log"

This command lists all .log files in the C:\Logs directory.

💡 Pro Tip: Always use -Filter when possible for better performance instead of piping to Where-Object.

Now, let me show you some real examples of the PowerShell Get-ChildItem cmdlet’s Filter parameter.

Check out How to Test If a File Exists in PowerShell?

Example 1: Filter by File Extension

To list all .txt files in a folder; you can use the -Filter parameter in the Get-ChildItem like below:

Get-ChildItem -Path "C:\Reports" -Filter "*.txt"

To include subdirectories:

Get-ChildItem -Path "C:\Reports" -Filter "*.txt" -Recurse

You can see the exact output in the screenshot below:

PowerShell Get-ChildItem Filter Examples

Example 2: Filter Only Files or Only Folders

To list only files; you can use the Get-ChildItem like below:

Get-ChildItem -Path "C:\Data" -File

To list only directories:

Get-ChildItem -Path "C:\Data" -Directory

Combine both for a recursive search:

Get-ChildItem -Path "C:\Data" -Directory -Recurse

Read PowerShell Out-File

Example 3: Filter by Date Modified

You can use Where-Object with the Get-ChildItem cmdlet to filter files by date:

Get-ChildItem -Path "C:\Logs" -File | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-7) }

This command lists files modified in the last 7 days.

Example 4: Filter by File Size

To find files larger than 1 MB:

Get-ChildItem -Path "C:\Downloads" -File | Where-Object { $_.Length -gt 1MB }

To find files smaller than 500 KB:

Get-ChildItem -Path "C:\Downloads" -File | Where-Object { $_.Length -lt 500KB }

Check out Find Files Modified After a Specific Date Using PowerShell

Example 5: Combine Filters (Extension + Size)

You can combine multiple filters using Where-Object:

Get-ChildItem -Path "C:\Projects" -Filter "*.csv" -Recurse |
  Where-Object { $_.Length -gt 2MB }

This lists .csv files larger than 2 MB in all subfolders.

Example 6: Using Regular Expressions (Regex)

If you need more advanced matching, use -Match with Where-Object:

Get-ChildItem -Path "C:\Data" -File | Where-Object { $_.Name -match '^Report_.*\.csv$' }

This finds files that start with “Report_” and end with “.csv”.

Example 7: Recursive Search with Depth Control

PowerShell 7+ introduces the -Depth parameter:

Get-ChildItem -Path "C:\Users\Public" -Recurse -Depth 2

This searches only two levels deep into subdirectories.

Read How to List Hidden Files in PowerShell?

Example 8: Exclude Certain Files or Folders

You can exclude files using Where-Object:

Get-ChildItem -Path "C:\Logs" -File | Where-Object { $_.Extension -ne ".bak" }

Or exclude specific folders:

Get-ChildItem -Path "C:\Logs" -Recurse | Where-Object { $_.FullName -notmatch "\\Archive\\" }

Example 9: Export Filtered Results to CSV

To save your filtered results in a CSV file, you can write the below PowerShell script.

Get-ChildItem -Path "C:\Reports" -Filter "*.csv" -Recurse |
  Select-Object Name, Length, LastWriteTime |
  Export-Csv -Path "C:\ReportsSummary.csv" -NoTypeInformation

This creates a CSV report of all .csv files with their size and modification date.

Example 10: Filter Hidden or System Files

To include hidden files:

Get-ChildItem -Path "C:\Windows" -Force

To show only hidden files:

Get-ChildItem -Path "C:\Windows" -Force | Where-Object { $_.Attributes -match "Hidden" }

The Get-ChildItem cmdlet is a must-know tool for any PowerShell user. Whether you’re automating system maintenance, managing logs, or auditing file structures, mastering filters will greatly improve your efficiency.

  • Use -Filter for performance.
  • Combine with Where-Object for complex logic.
  • Use -Recurse and -Depth for flexible searches.
  • Always test your filters before applying large-scale changes.

Do let me know if these PowerShell Get-ChildItem Filter examples help you.

You may also like:

Leave a Comment

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.