Quickly delete large folders with PS


i have script right goes through , deletes list of directories (old users home directories).  fine , here code sample:

get-content $usersfile | foreach-object {      $pathcheck = test-path "$basepath\$_"      if ($pathcheck -eq $false){          write-host "'$basepath\$_' not found"          logwrite "'$basepath\$_' not found"      } else {          remove-item "$basepath\$_" -recurse -force -verbose          write-host "$scriptcontext has removed '$basepath\$_'"          logwrite "$scriptcontext has removed '$basepath\$_'"      }  }

can see i'm using "remove-item <path> -recurse -force -verbose" delete targeted folder , of it's contents.  while working, seems take longer delete single large folder if right-click on large folder , delete permanently.  there faster way delete directory , of contents?

when "large" folders, how large large?

maybe adding in progress indicator enough:

http://technet.microsoft.com/en-us/magazine/2008.03.powershell.aspx?pr=blog

https://blogs.technet.com/b/heyscriptingguy/archive/2011/01/29/add-a-progress-bar-to-your-powershell-script.aspx?redirected=true


don't retire technet!

i guess @ companies it's better tell task take 30 minutes , show them progress, instead of getting task take 5 minutes.  don't see solution however.

ed wilson in awesomness (it's word), wrote article able find on different deletion methods here:

http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/22/the-best-way-to-use-powershell-to-delete-folders.aspx

i combined measure-command see of 3 methods more effective against a sample 3gb folder on network variety of file types , sub-folders.  here script:

cls write-host "method 1: use native cmdlets" measure-command {remove-item -path "\\<server>\home$\deltest1" -recurse -force}  write-host "method 2: filesystemobject still works" $fso = new-object -comobject scripting.filesystemobject measure-command {$fso.deletefolder("\\<server>\home$\deltest2",$true)}  write-host "method 3: use .net classes" measure-command {dir "\\<server>\home$\deltest3*" | foreach { [io.directory]::delete($_.fullname,$true) }}

and output:

days              : 0 hours             : 0 minutes           : 2 seconds           : 40 milliseconds      : 206 ticks             : 1602064644 totaldays         : 0.00185424148611111 totalhours        : 0.0445017956666667 totalminutes      : 2.67010774 totalseconds      : 160.2064644 totalmilliseconds : 160206.4644  method 2: filesystemobject still works days              : 0 hours             : 0 minutes           : 1 seconds           : 43 milliseconds      : 827 ticks             : 1038275004 totaldays         : 0.00120170718055556 totalhours        : 0.0288409723333333 totalminutes      : 1.73045834 totalseconds      : 103.8275004 totalmilliseconds : 103827.5004  method 3: use .net classes  days              : 0 hours             : 0 minutes           : 1 seconds           : 46 milliseconds      : 254 ticks             : 1062543372 totaldays         : 0.00122979556944444 totalhours        : 0.0295150936666667 totalminutes      : 1.77090562 totalseconds      : 106.2543372 totalmilliseconds : 106254.3372

can see, specific case, method 2 using file systemobject fastest.  i'll re-running several times see if can build trend, think sufficient identify method fastest.


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

difference between wuauclt1.exe and wuauclt.exe

Windows 2016 RDS event 1306 Connection Broker Client failed to redirect the user... Error: NULL