07-21-2023, 08:28 PM
while rm -r yields good results, the following method is faster:
$fso = New-Object -ComObject scripting.filesystemobject
$fso.DeleteFolder("D:\folder_to_remove")
To test this you can easily create a folder with X files (I used: [Disk Tools][1] to quickly generate the files).
And then run each of the variants using:
Measure-Command {rm D:\FOLDER_TO_DELETE -r}
Measure-Command {Remove-Item -Path D:\FOLDER_TO_DELETE -Recurse -Force}
Measure-Command {rd -r FOLDER_TO_DELETE }
$fso.DeleteFolder("D:\folder_to_remove")
Measure-Command {$fso.DeleteFolder("D:\FOLDER_TO_DELETE")}
the results on my test folder were:
Remove-Item - TotalMilliseconds : 1438.708
rm - TotalMilliseconds : 1268.8473
rd - TotalMilliseconds : 739.5385
FSO - TotalMilliseconds : 676.8091
The results vary but on my system the winner was the fileSystemObject. I recommend testing this on the target file system to see which method is the best for you.
[1]:
$fso = New-Object -ComObject scripting.filesystemobject
$fso.DeleteFolder("D:\folder_to_remove")
To test this you can easily create a folder with X files (I used: [Disk Tools][1] to quickly generate the files).
And then run each of the variants using:
Measure-Command {rm D:\FOLDER_TO_DELETE -r}
Measure-Command {Remove-Item -Path D:\FOLDER_TO_DELETE -Recurse -Force}
Measure-Command {rd -r FOLDER_TO_DELETE }
$fso.DeleteFolder("D:\folder_to_remove")
Measure-Command {$fso.DeleteFolder("D:\FOLDER_TO_DELETE")}
the results on my test folder were:
Remove-Item - TotalMilliseconds : 1438.708
rm - TotalMilliseconds : 1268.8473
rd - TotalMilliseconds : 739.5385
FSO - TotalMilliseconds : 676.8091
The results vary but on my system the winner was the fileSystemObject. I recommend testing this on the target file system to see which method is the best for you.
[1]:
[To see links please register here]