- 1 year ago
How to use Robocopy in a Powershell for loop using arrays?
Comment
1 / 1
To use Robocopy in a PowerShell for loop with arrays, you can iterate over the arrays and call Robocopy within the loop. Here's an example:
powershell$sourceFolders = @("C:\Folder1", "C:\Folder2", "C:\Folder3")
$destinationFolder = "D:\Destination"
foreach ($sourceFolder in $sourceFolders) {
# Construct the Robocopy command
$robocopyArgs = "$sourceFolder $destinationFolder /E /COPYALL"
# Run Robocopy
Start-Process -FilePath "robocopy" -ArgumentList $robocopyArgs -Wait
}
In the above example, $sourceFolders
is an array of source folders, and $destinationFolder
is the destination folder. The foreach
loop iterates over each source folder in the array, and for each iteration, it constructs the Robocopy command with the appropriate source and destination paths. The Robocopy command is then executed using Start-Process
with the -Wait
parameter to wait for the process to complete before moving to the next iteration.
Adjust the source and destination paths and any other Robocopy options according to your requirements.