Powershell with Robocopy and Arguments Passing
Start robocopy -args "$source $destination $fileLIst $robocopyOptions"
orrobocopy $source $destination $fileLIst $robocopyOptions.split(' ')
Use the arrays, Luke. If you specify an array of values, PowerShell will automatically expand them into separate parameters. In my experience, this is the most reliable method. And it doesn't require you to mess with the Start-Process
cmdlet, which is in my opinion is overkill for such tasks.
This trick is from the best article I've seen on the PowerShell behavior towards external executables: PowerShell and external commands done right.
Example:
$source = 'C:\hold\first test'$destination = 'C:\hold\second test'$robocopyOptions = @('/NJH', '/NJS')$fileList = 'test.txt'$CmdLine = @($source, $destination, $fileList) + $robocopyOptions& 'robocopy.exe' $CmdLine
You can't use a string to pass options in that way because when you write
robocopy $source $destination $fileList $robocopyOptions
PowerShell will evaluate the last variable ($robocopyOptions
) as a single string and it will quote it. This means robocopy will get "/NJH /NHS"
(single string, quoted) on its command line. (Obviously not the intent.)
For details on how to work around these kinds of issues, see here:
http://windowsitpro.com/powershell/running-executables-powershell
The article includes 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 function will let you run an executable in the current console window and also let you build an array of string parameters to pass to it.
So in your case you could use this function something like this:
Start-Executable robocopy.exe $source,$destination,$fileList,$robocopyOptions