Powershell - Search for Files
Only current directory, no subdirectories, any matching files:
Get-ChildItem *.txt
Specify directory and recursive:
Get-Childitem -Path C:\Users\user1 -Recurse
Search two directories:
Get-Childitem -Path C:\Users\user1, D:\Steaam -Recurse
Get-Childitem -Path C:\Users\user1\Documents, C:\Users\user1\Videos -Recurse
Useful example with most options:
Get-Childitem –Path C:\ -Include *.mkv* -Exclude *test* -File -Recurse -force -ErrorAction SilentlyContinue
Options:
-File # only files, not dirs
-Path C:\ # start searching here
-Include *.mkv* # include any matching filev
-Exclude *test* # exclude any matching file
-Recurse # recursive - show sub dir contents
-force # include hidden dirs
-ErrorAction SilentlyContinue # ignore minor errors like unreadable permissions
Anything after a certain date:
$TheDate=Get-Date -Year 2021 -Month 08 -Day 18
Get-Childitem –Path C:\Users\user1 -Recurse | Where-Object { $_.LastWriteTime -ge $TheDate }
Anything during a one day period after a certian date:
$TheDate=Get-Date -Year 2021 -Month 08 -Day 18
Get-Childitem –Path C:\Users\user1 -Recurse | Where-Object { $_.LastWriteTime -ge $TheDate -and $_.LastWriteTime -le $TheDate.adddays(1) }
Anything greater than 25KB:
Get-ChildItem | Where-Object {$_.Length -gt 25KB}
Everything older than 30 days:
Get-ChildItem | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)}
Examples before, after, and between specific dates:
Get-ChildItem | Where-Object {$_.LastWriteTime -lt '7/7/2023'}
Get-ChildItem | Where-Object {$_.LastWriteTime -gt '7/7/2023'}
Get-ChildItem -Recurse | Where-Object {$_.LastWriteTime -lt '7/7/2023'}
Get-ChildItem -Recurse | Where-Object {$_.LastWriteTime -lt '7/8/2023' -and $_.LastWriteTime -gt '7/7/2023'}