I'm looking for a way to automatically count specific files, like anything with '859' in the name, across multiple drives every month and log the results to a file. I have a PowerShell script that uses Get-ChildItem to search through the directories, but I'm not sure how efficient it is for large searches. Here's what I currently have:
```powershell
Get-ChildItem -LiteralPath 'C:' -Filter "*859*" -Recurse | Select-Object -ExpandProperty FullName | Out-File 'C:LogFileLocation.txt'
```
I also want to include a date range in the log. Any tips on improving performance, or alternative methods?
5 Answers
You might want to just run your script and time it! Get-ChildItem isn't the fastest, especially when scanning the whole C: drive, so expect it to take a while. ?
For quick file counts, you could dump the Master File Table (MFT) instead of scanning the entire filesystem; it’s way faster. Here's a GitHub link to a tool that might help: Mft2Csv.
Try using System.IO.Directory's GetFiles method instead! It's generally more efficient than Get-ChildItem because it doesn't convert files into objects. Here’s a quick example you can tweak:
```powershell
$Path = "C:"
$Filter = "*859*"
$OutFile = "C:LogfileLocation.txt"
$EnumerationOptions = [System.IO.EnumerationOptions]::new()
$EnumerationOptions.IgnoreInaccessible = $True
$EnumerationOptions.RecurseSubdirectories = $True
[System.IO.Directory]::GetFiles($Path, $Filter, $EnumerationOptions) | Out-File $OutFile
```
Have you considered using an indexing tool like Everything? It's super quick and has command-line options for searching files, which could save you a lot of time! Check it out [here](https://www.voidtools.com/support/everything/).
If your directories remain unchanged, why not create an array with the paths and iterate through those? This can significantly speed things up as you won't need to search all of C: recursively every time.
Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically