Just a few quick notes on how to search for files on Windows using native powershell commands.
Get-ChildItem
Is the key. By default this just gives us the same output as boring old ‘dir’ from our cmd.exe days but with the large set of options it has we can use it as a file finding powerhouse. Under pwsh you can also just use ‘dir’ or ’ls’ which are aliases for this.
Get-Childitem -ErrorAction SilentlyContinue –Path C:\ -Recurse -force
The key here is using the ‘-Recurse’ with ‘-ErrorAction SilentlyContinue’. This lets us not be overwhelmed by a ton of errors for parts of the filesystem we don’t have permissions to see.
The ‘-force’ lets us traverse hidden directories.
Some other useful options:
- -Include only include items that match the pattern
- -Exclude exclude items that match
- -File show only files
Using this as the base we can now find what we are looking for on our system.
We can also combine this with Where-Object to look at other file attributes like dates.
First pick a date we want to use:
$MyDate=Get-Date -Year 2022 -Month 06 -Day 01
Find files written since the first of the month
Get-Childitem -ErrorAction SilentlyContinue –Path . -Recurse -force | Where-Object { $_.LastWriteTime -ge $MyDate }