Description
The copyfile_preservemetadata.ps1 PowerShell script copies files from a specified source directory to a destination directory while preserving the original date and time attributes of the files. It utilizes the robocopy command to handle the copy operation, which includes an option for specifying file types to be copied.
Parameters
- sourcePath [Required]: The path to the source directory containing the files to be copied.
- destinationPath [Required]: The path to the destination directory where the files will be copied.
- filetypes (String) : Specifies the file types to be copied (e.g., *.txt, *.jpg). If not provided, all file types (*.*) are copied by default.
File Copy Operation
- Uses the robocopy command to copy files from the source to the destination. The parameters passed to robocopy include:
- $sourcePath: Source directory.
- $destinationPath: Destination directory.
- $filetypes: File types to copy.
- /COPYALL: Copies all file attributes, including data, attributes, timestamps, security (ACLs), and ownership.
Error Handling
- Checks the exit code of robocopy using $LASTEXITCODE.
- If the exit code is less than or equal to 3, it indicates a successful operation, and a success message is displayed.
- If the exit code is greater than 3, it indicates an error, and an error message is displayed with the exit code.
Example
- Open PowerShell as administrator from start menu.
- powershell -ExecutionPolicy Bypass -File “[powershellscript]” -sourcePath “[sourcepath]” -destinationPath “[destinationpath]” -filetypes “*.docx”
- powershell -ExecutionPolicy Bypass -File “[fullpath] \ copyfile_preservemetadata.ps1” -sourcePath “D:\word” -destinationPath “D:\\word” -filetypes “*.docx”
Reviews
There are no reviews yet.