Powershell with Robocopy and Arguments Passing

weloytty picture weloytty · Apr 10, 2015 · Viewed 31.2k times · Source

I'm trying to write a script that uses robocopy. If I were just doing this manually, my command would be:

robocopy c:\hold\test1 c:\hold\test2 test.txt /NJH /NJS

BUT, when I do this from powershell, like:

$source = "C:\hold\first test"
$destination = "C:\hold\second test"
$robocopyOptions = " /NJH /NJS "
$fileList = "test.txt"

robocopy $source $destination $fileLIst $robocopyOptions

I get:

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

  Started : Fri Apr 10 09:20:03 2015

   Source - C:\hold\first test\
     Dest - C:\hold\second test\

    Files : test.txt

  Options : /COPY:DAT /R:1000000 /W:30

------------------------------------------------------------------------------

ERROR : Invalid Parameter #4 : " /NJH /NJS "

However, if I change the robocopy command to

robocopy $source $destination $fileLIst  /NJH /NJS 

everything runs successfully.

So, my question is, how can I pass a string as my robocopy command options (and, in a larger sense, do the same for any given external command)

Answer

Anton Z picture Anton Z · Apr 10, 2015

Start robocopy -args "$source $destination $fileLIst $robocopyOptions"
or
robocopy $source $destination $fileLIst $robocopyOptions.split(' ')