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
-Filterinstead ofWhere-Objectwhen possible (faster) - Use
-Fileor-Directoryto exclude unwanted types - Limit
-Recursedepth with-Depthparameter - Use
Select-Objectto 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
-Depthor-Filter
Related Articles
File Management Operations
- PowerShell Delete All Files - Delete files
- PowerShell Rename Files - Rename files in bulk
- PowerShell Delete Files Matching Pattern - Pattern-based deletion
- PowerShell Delete File If Exists - Conditional deletion
- PowerShell Count Files - Count directory contents
Directory Operations
- PowerShell Directory Listing - Navigate directories
- PowerShell Get Folder Size - Calculate folder size
- PowerShell Get Folder Creation Date - Folder timestamps
File Information & Properties
- PowerShell Get File Properties - File metadata
- PowerShell Get File Extension - Extract extension
- PowerShell Get File Hash - File integrity checking
- PowerShell Get File Version - Version information
- PowerShell Get File Owner - File ownership
- PowerShell Get MD5 Hash - Hash calculation
Date & Time Operations
- PowerShell DateTime Format - Format date/time
- PowerShell Compare File Dates - Date comparison
- PowerShell Change Created Date - Modify creation date
- PowerShell Change Last Modified Date - Modify modification date
- PowerShell List Files By Date - Sort by date
- PowerShell Subtract Dates - Date arithmetic
Data Selection & Filtering
- PowerShell Select-Object - Select file properties
- PowerShell Where-Object - Filter files by criteria
- PowerShell ForEach-Object - Process each file
- PowerShell Get-ChildItem Filter - Advanced filtering
- PowerShell Get-ChildItem Regex - Regex filtering
Data Calculation & Aggregation
- PowerShell Measure-Object - Calculate statistics
- PowerShell Group-Object - Group files by properties
Display & Formatting
- PowerShell Format Table - Format output table
- PowerShell Output Table - Create data tables
- PowerShell Format List - List format display
- PowerShell Write-Output - Display output
Control Flow & Logic
- PowerShell If-Else Statement - Conditional logic
- PowerShell Switch Statement - Switch logic
- PowerShell For Loops - Loop through files
- PowerShell Try-Catch - Error handling
Variables & Collections
- PowerShell Variables - Store file data
- PowerShell Arrays - Work with file arrays
- PowerShell Hashtables - Store mappings
- PowerShell Add-Member - Add custom properties
String & Pattern Operations
- PowerShell Strings - String manipulation
- PowerShell Replace Strings - String replacement
- PowerShell Replace Text - Text operations
Output & Export
- PowerShell Output to File - Write to files
- PowerShell Export CSV - Export to CSV
- PowerShell Import CSV - Import from CSV
File Content Operations
- PowerShell Get-Content - Read file contents
- PowerShell Get First Line - Read first line
- PowerShell Get-Content Skip First Line - Skip lines
- PowerShell Get-Content Remove First Line - Remove header
Functions & Automation
- PowerShell Functions - Create reusable functions
- PowerShell Measure-Object - Calculate totals
Comprehensive Guides
- Complete PowerShell Guide - Full PowerShell reference
- Complete PowerShell Tutorial - Comprehensive course