You cannot use a string to pass parameters this way, because when you write
robocopy $source $destination $fileList $robocopyOptions
PowerShell will evaluate the last variable ( $robocopyOptions ) as one line, and it will quote it. This means that robocopy will receive "/NJH /NHS" (single line, quotation mark) at the command line. (Obviously, this is not an intention.)
For more information on how to get around these issues, see here:
http://windowsitpro.com/powershell/running-executables-powershell
The article contains the following function:
function Start-Executable { param( [String] $FilePath, [String[]] $ArgumentList ) $OFS = " " $process = New-Object System.Diagnostics.Process $process.StartInfo.FileName = $FilePath $process.StartInfo.Arguments = $ArgumentList $process.StartInfo.UseShellExecute = $false $process.StartInfo.RedirectStandardOutput = $true if ( $process.Start() ) { $output = $process.StandardOutput.ReadToEnd() ` -replace "\r\n$","" if ( $output ) { if ( $output.Contains("`r`n") ) { $output -split "`r`n" } elseif ( $output.Contains("`n") ) { $output -split "`n" } else { $output } } $process.WaitForExit() & "$Env:SystemRoot\system32\cmd.exe" ` /c exit $process.ExitCode } }
This feature will allow you to run the executable in the current console window, and also allow you to build an array of string parameters to pass it.
So, in your case, you can use this function something like this:
Start-Executable robocopy.exe $source,$destination,$fileList,$robocopyOptions
source share