Powershell with Robocopy and Arguments Passing

后端 未结 4 848
粉色の甜心
粉色の甜心 2021-01-02 04:09

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\\t         


        
4条回答
  •  我在风中等你
    2021-01-02 04:45

    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
    

提交回复
热议问题