How can I automate monthly file counting across a server?

0
1
Asked By FileHunter92 On

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

Answered By CodeNinja77 On

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. ?

Answered By FastTrackDev On

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.

Answered By EfficientCoder On

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
```

Answered By TechWhiz99 On

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/).

Answered By PowerScripter88 On

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.