Jun 09

PS One-Liner: #5 Create multithreaded processes

This one is a golden oldie and used for migrating data using robocopy.  Let’s start with a simple example.

First we begin with an array ”@(notepad…)’ where we define the processes to start. Then we use foreach ‘%’ which fetches each item in the array as a pipeline item ‘$_’ and then checks how many notepad processes there are ‘(gps $_).count -ge 1’ , where ‘gps’ is the alias for ‘get-process’. While there are equal or greater then ‘-ge’  1 notepad process then sleep for 5 seconds and check again. So when there is no ‘0’ notepad process left  it breaks out of the while loop and executes a new powershell line ‘;’ with ‘start $_’. Start is the alias for ‘start-process’. And of course since we use foreach it takes the next entry and does that again until there is no entry left in the array.

RoboCopy on Speed

But what if you want to do the same thing with robocopy, since we want to use parameters with robocopy I replaced ‘start-process’ with WMI process create (executes process in cmd) which allows me to execute everything on one line without seperating the parameters and I used gc ‘get-content’ instead of an array which in fact also creates an array of the text in the batch file. I also adjusted the process count based on the CPU usage and Disk I/O of the migration box in question and finally ended up with the example below.

robocopy x:\location1 d:\location1 /mir /R:1 /W:1 /NS /NC /nfl /zb
robocopy x:\location2 d:\location2 /mir /R:1 /W:1 /NS /NC /nfl /zb


I also wanted to note that robocopy is part of my toolbelt for years, in fact since I started with Windows NT. RoboCopy is a true lifesaver and it’s founder ‘Kevin Allen’ deserves the nobel prize when it comes to making the Windows Admin life a whole lot easier. So a big shout out to Kevin Allen, wherever you are!