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 therobocopycommand.$robocopyParams = @(...): Creates an array variable named$robocopyParamsthat holds the parameters for therobocopycommand.$sourcePath,$destinationPath: The source and destination paths are added as individual elements to the$robocopyParamsarray."/E", "/COPYALL": The/Eswitch copies all subdirectories, including empty ones. The/COPYALLswitch copies all file information including timestamps, attributes, and NTFS security permissions. These switches are added as separate elements to the$robocopyParamsarray."/LOG:$logPath": The/LOGswitch specifies the log file path. The$logPathvariable is added as part of the element in the$robocopyParamsarray.Robocopy @robocopyParams: Executes therobocopycommand using the splatting technique, passing the individual elements of the$robocopyParamsarray as parameters to therobocopycommand.