How can I automate monthly file counting across a server?
Asked by
FileHunter92
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?
Answers
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.
Reply to this answer
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/).
Reply to this answer
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
```
Reply to this answer
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.
Reply to this answer
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. ?
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions