Skip to main content

PowerShell List Files in Directory: Complete Guide with Filtering [2024]

4 min read
powershell get-childitem list files directory ls dir filtering

Master PowerShell file listing with Get-ChildItem—list files, directories, recurse, and filter efficiently. Complete guide with real-world patterns.

Quick Syntax

Get-ChildItem -Path "C:\temp" -File              # Files only
Get-ChildItem -Path "C:\temp" -Directory         # Directories only
Get-ChildItem -Path "C:\temp" -Recurse           # All subdirectories

List Contents of Directory

Current Directory

Get-ChildItem              # Current directory
ls                         # Alias
dir                        # Alias

Specific Directory

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

Filter by Type

Files Only

Get-ChildItem -Path "C:\temp" -File
Get-ChildItem -Path "C:\temp" -Attributes !Directory

Directories Only

Get-ChildItem -Path "C:\temp" -Directory
Get-ChildItem -Path "C:\temp" -Attributes Directory

Recursive Listing

All Subdirectories

Get-ChildItem -Path "C:\Logs" -Recurse -File

Limit Recursion Depth

# PowerShell 5.1+
Get-ChildItem -Path "C:\Logs" -Recurse -Depth 2 -File

Filter by Extension

Single Extension

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

Multiple Extensions

Get-ChildItem -Path "C:\Temp" -File |
    Where-Object { $_.Extension -in ".txt", ".log", ".csv" }

Filter by Name Pattern

Wildcard Pattern

Get-ChildItem -Path "C:\Backup" -Filter "backup_*.bak"
Get-ChildItem -Path "C:\Reports" -Filter "*2024*"

Regex Filter

Get-ChildItem -Path "C:\Logs" -File |
    Where-Object { $_.Name -match "app_\d{4}\.log" }

Filter by Size

Large Files

Get-ChildItem -Path "C:\temp" -File |
    Where-Object { $_.Length -gt 100MB }  # > 100MB

File Size Range

Get-ChildItem -Path "C:\temp" -File |
    Where-Object { $_.Length -gt 10MB -and $_.Length -lt 100MB }

Sort Results

By Name

Get-ChildItem -Path "C:\temp" | Sort-Object -Property Name

By Date (Newest First)

Get-ChildItem -Path "C:\Logs" -File |
    Sort-Object -Property LastWriteTime -Descending | Select-Object -First 10

By Size

Get-ChildItem -Path "C:\temp" -File |
    Sort-Object -Property Length -Descending | Select-Object -First 5

Format Output

Table View

Get-ChildItem -Path "C:\temp" | Format-Table -AutoSize Name, Length, LastWriteTime

List View

Get-ChildItem -Path "C:\temp" -File | Select-Object Name, @{Name="SizeMB"; Expression={[Math]::Round($_.Length/1MB,2)}}, LastWriteTime

Real-World Examples

Find Large Old Files

Get-ChildItem -Path "C:\Logs" -File -Recurse |
    Where-Object { $_.Length -gt 50MB -and $_.LastWriteTime -lt (Get-Date).AddDays(-30) } |
    Select-Object FullName, @{Name="SizeMB"; Expression={[Math]::Round($_.Length/1MB,2)}}, LastWriteTime

List Recent Files

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

Find Duplicate Extensions

Get-ChildItem -Path "C:\Temp" -Recurse -File |
    Group-Object -Property Extension |
    Where-Object { $_.Count -gt 1 }

Performance Tips

  • Use -Filter instead of Where-Object when possible (faster)
  • Use -File or -Directory to exclude unwanted types
  • Limit -Recurse depth with -Depth parameter
  • Use Select-Object to choose specific columns

Common Mistakes

Issue: “Cannot find path”

  • Cause: Typo in path
  • Solution: Verify path with Test-Path

Issue: “Access denied”

  • Cause: No permissions
  • Solution: Run as admin

Issue: Slow recursion

  • Cause: Large directory tree
  • Solution: Use -Depth or -Filter

File Management Operations

Directory Operations

File Information & Properties

Date & Time Operations

Data Selection & Filtering

Data Calculation & Aggregation

  • PowerShell Measure-Object - Calculate statistics
  • PowerShell Group-Object - Group files by properties

Display & Formatting

Control Flow & Logic

Variables & Collections

String & Pattern Operations

Output & Export

File Content Operations

Functions & Automation

Comprehensive Guides