Using RoboCopy with PowerShell – Parameterized
The code sets the source path, destination path, and log file path using variables. It creates an array variable ($robocopyParams
) that holds the parameters for the robocopy
command, including the source, destination, and log file paths. Finally, it executes the robocopy
command using the parameters from the $robocopyParams
array.
$sourcePath = "C:\Path\to\source"
$destinationPath = "C:\Path\to\destination"
$logPath = "C:\Path\to\log\file.log"
$robocopyParams = @(
$sourcePath,
$destinationPath,
"/E",
"/COPYALL",
"/LOG:$logPath"
)
Robocopy @robocopyParams
$sourcePath = "C:\Path\to\source"
: Sets the source path of the files or directories you want to copy.$destinationPath = "C:\Path\to\destination"
: Sets the destination path where the files or directories will be copied to.$logPath = "C:\Path\to\log\file.log"
: Specifies the path and filename for the log file that will capture the output of therobocopy
command.$robocopyParams = @(...)
: Creates an array variable named$robocopyParams
that holds the parameters for therobocopy
command.$sourcePath
,$destinationPath
: The source and destination paths are added as individual elements to the$robocopyParams
array."/E", "/COPYALL"
: The/E
switch copies all subdirectories, including empty ones. The/COPYALL
switch copies all file information including timestamps, attributes, and NTFS security permissions. These switches are added as separate elements to the$robocopyParams
array."/LOG:$logPath"
: The/LOG
switch specifies the log file path. The$logPath
variable is added as part of the element in the$robocopyParams
array.Robocopy @robocopyParams
: Executes therobocopy
command using the splatting technique, passing the individual elements of the$robocopyParams
array as parameters to therobocopy
command.