PowerShell: search files by name, regex or content
Get-ChildItem and Select-String cheat sheet to find files.
PowerShell cheat sheet to search for files by name, by regular expression or by content.
Search by name
Get-ChildItem -Path C:\ -Include *Dynamics365* -File -Recurse -ErrorAction SilentlyContinue
With exclusions:
Get-ChildItem -Path C:\ -Include *report* -Exclude *.JPG,*.MP3,*.TMP -File -Recurse -ErrorAction SilentlyContinue
Filter by regular expression on the name
Get-ChildItem | Where-Object { $_.Name -match '^KB[0-9]+\.log$' }
Search inside file contents
Get-ChildItem c:\*.txt -Recurse | Select-String -Pattern EX
Get-ChildItem c:\*.txt -Recurse | Select-String -Pattern "EX.*E"
List .js file names by descending size
The names (without extension) are copied to the clipboard:
Get-ChildItem -Path .\ -Filter *.js -Recurse -File | Sort-Object Length -Descending | ForEach-Object {
$_.BaseName
} | clip
Notes
-Includeonly works as expected with-Recurseor a path ending with a wildcard.-ErrorAction SilentlyContinuehides access-denied errors.