1

I'm having some trouble using RoboCopy.exe through a Powershell script. The function is as follows...

function copyFiles{
    Start-Process "RoboCopy.exe" -ArgumentList "'C:\Test' 'C:\test2' /copyall"
    Write-Host 'Test'
}

"Test" is output to console but the copy does not run. I was initially attempting to copy with variables as the directories, but I've boiled it down to this since that was not working. In the directory 'test1' there is a single .txt file and 'test2' exists but is empty. I have tried it without that directory existing as well.

It is called in another file via:

. .\rCopy.ps1
copyFiles

Now, I tried outputting the log to a file with the code below, but it gives me a permissions error. "Out-File : Access to the path 'C:\Users\Me\Desktop' is denied". I also tried outputting direct to C:\ but that gives the same error. Powershell IS running as administrator.

$testCopy = Start-Process "RoboCopy.exe" -ArgumentList "'C:\Test' 'C:\test2' /copyall"
$testCopy | Out-File -LiteralPath "C:\Users\Me\

And to close, I have also tried running "Start-Process 'RoboCopy.exe' -ArgumentList 'C:\test' 'C:\test2' /copyall" in the main script, without calling the function from another.

Any help would be greatly appreciated.

MalyG
  • 301
  • 1
  • 6
  • 15
  • Have you tested the robocopy syntax outside of powershell? Does the destination directory already exists? Out-File requires a filename, not a path. Make sure you have rights to copy files to destination, or in this case c: – Jawad Feb 16 '20 at 18:18
  • As an aside: Your use of `Start-Process` may be intentional to run `RocoCopy.exe` _asynchronously_, in a _new window_, but the more typical case is to execute console applications _synchronously_, in the _same window_, in which case you need to call them _directly_ (`c:\path\to\some.exe ...` or `& $exePath ...`) - see [this answer](https://stackoverflow.com/a/51334633/45375). An alternative to asynchronous execution in a new window is to use a [background job](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Jobs). – mklement0 Feb 16 '20 at 19:27

2 Answers2

1

I was able to get it working by using the following syntax:

Start-Process "RoboCopy.exe" -argumentlist "`"$localDir`" `"$networkDir`"  "

I cannot copy from the directory I've been trying to from the get-go, but I'm assuming that's a permissions issue.

MalyG
  • 301
  • 1
  • 6
  • 15
0

Your own solution is effective: quoted arguments passed to external programs such as Robocopy.exe inside a (single) -ArgumentList argument require embedded double quotes ("), not single quotes (').

Generally, external programs do not recognize ' as string delimiters on the command line, and that even applies to calling PowerShell itself via its CLI's -File parameter.

Even if you were to pass the arguments one by one to -ArgumentList:

# Note the individual arguments.
# !! Works in this case, but with paths that have *embedded spaces*
# !! you  would still need embedded "..." quoting.
Start-Process Robocopy.exe -ArgumentList 'C:\Test', 'C:\test2', '/copyall'

you'd still have to use embedded "..." quoting in individual arguments that (may) contain embedded spaces (e.g., '"C:\Test 1"' or, with a variable reference, "`"$dir`"", due to a long-standing bug that won't be fixed for the sake of backward compatibility - see GitHub issue #5576.

See this answer for background information.

mklement0
  • 382,024
  • 64
  • 607
  • 775