Fastest way to Count Large Number of Files (1M+) in a Folder


Requirement is to report how many files got imported to a folder after a lengthy db action.

Folder properties is taking over an hour and counting. I thought PS might be able to jig it up a bit.

Fastest code I can think of today is:


([System.IO.Directory]::GetFiles($path,'*.*','AllDirectories')) | Measure-Object | %{$_.Count}

Currently taking about 15 minutes to return 3 million+ files.

Any faster code ideas welcome.

You could try utilizing robocopy with it’s /L switch.

robocopy “SourceFolder” “DummyTargetFolder” /S /XJ /L /NP /NFL /NDL


1000 thanks Sir - using Robocopy seems to be way faster, around 4-6 seconds for similar sizes of data.