1
votes

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.

2
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
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. An alternative to asynchronous execution in a new window is to use a background job. - mklement0

2 Answers

1
votes

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.

0
votes

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.

See this answer for background information.