Get file list with Robocopy Get file list with Robocopy powershell powershell

Get file list with Robocopy


If you're just looking for a faster way to get that list of files, the legacy dir command will do that:

$files = cmd /c dir c:\aaa /b /s /a-dforeach ($file in $files){...}

Edit: Some comparative performance tests-

(measure-command {gci -r |? {-not $_.psiscontainer } }).TotalMilliseconds(measure-command {gci -r -file}).TotalMilliseconds(measure-command {(robocopy . NULL /l /s /ndl /xx /nc /ns /njh /njs /fp) }).TotalMilliseconds(measure-command {cmd /c dir /b /s /a-d }).TotalMilliseconds627.5434417.8881299.906986.9364

The tested directory had 6812 files in 420 sub-directories.


$array = $files -split '\r?\n'

I'm assuming $files is text separated by line breaks. This will split the string by line breaks and assign to $array.


Looks like the Robocopy output is troubled by white space. This works:

(robocopy . NULL /l /s /ndl /xx /nc /ns /njh /njs /fp) | % {gci $_.trim()}

Whether it is quicker depends on how you filter for files. If your PS version supports -file for the gci cmdlet (therefore handing the filtering to the file system provider), then PS is fastest. Using Where-Object roughly doubles that time whilst Robocopy is somewhere in between (for this example of 240 files):

measure-command {gci -r -file}Days              : 0Hours             : 0Minutes           : 0Seconds           : 0Milliseconds      : 24Ticks             : 245746TotalDays         : 2.84428240740741E-07TotalHours        : 6.82627777777778E-06TotalMinutes      : 0.000409576666666667TotalSeconds      : 0.0245746TotalMilliseconds : 24.5746measure-command  {gci -r | ? {$_.PSIsContainer -eq $false}}Days              : 0Hours             : 0Minutes           : 0Seconds           : 0Milliseconds      : 48Ticks             : 480647TotalDays         : 5.56304398148148E-07TotalHours        : 1.33513055555556E-05TotalMinutes      : 0.000801078333333333TotalSeconds      : 0.0480647TotalMilliseconds : 48.0647measure-command {(robocopy . NULL /l /s /ndl /xx /nc /ns /njh /njs /fp)}Days              : 0Hours             : 0Minutes           : 0Seconds           : 0Milliseconds      : 36Ticks             : 365689TotalDays         : 4.23251157407407E-07TotalHours        : 1.01580277777778E-05TotalMinutes      : 0.000609481666666667TotalSeconds      : 0.0365689TotalMilliseconds : 36.5689