Power Shell script that copies a file while preserving its metadata.

$0.00

Category:

Product Overview

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. This tool is essential for legal professionals and eDiscovery teams who need to maintain file integrity and metadata during document transfer operations.

Key Features

🕐

Metadata Preservation

Maintains original timestamps, attributes, and file properties during copy operations

📁

Selective File Types

Filter and copy specific file types with wildcard pattern support

🔒

Complete Attribute Copy

Copies data, attributes, timestamps, security (ACLs), and ownership information

Robust Error Handling

Built-in validation and clear error reporting for reliable operations

Parameters

📂

sourcePathRequired

String Parameter

The path to the source directory containing the files to be copied. This must be a valid directory path that exists and is accessible from the system running the script.

Type String (Directory Path)
Format Absolute or relative path to source directory
Validation Directory must exist and be readable
Example Values

Local Path: D:\Documents
Network Path: \\server\share\files
Relative Path: .\SourceFiles

📁

destinationPathRequired

String Parameter

The path to the destination directory where the files will be copied. If the destination directory does not exist, robocopy will create it automatically during the copy operation.

Type String (Directory Path)
Auto-Create Yes – Directory will be created if it doesn’t exist
Permissions Must be writable by the executing user
Example Values

Local Path: D:\Backup\Documents
Network Path: \\backup-server\archive\files
New Directory: C:\NewBackup (will be created)

🎯

filetypesOptional

String Parameter

Specifies the file types to be copied using wildcard patterns (e.g., *.txt, *.jpg). If not provided, all file types (*.*) are copied by default. This parameter allows for selective copying based on file extensions.

Type String (Wildcard Pattern)
Default Value *.* (all files)
Pattern Support Standard wildcard patterns (* and ?)
Example Values

Documents: *.docx or *.pdf
Images: *.jpg or *.png
Text Files: *.txt
All Files: *.* (default if omitted)

Technical Details

📋 File Copy Operation

The script uses the robocopy command to copy files from the source to the destination. Robocopy (Robust File Copy) is a Windows command-line utility designed for reliable file copying with extensive options for preserving file attributes and handling errors.

Robocopy Parameters Used:

$sourcePath
Source directory from which files are copied
$destinationPath
Destination directory where files are copied to
$filetypes
File type filter for selective copying
/COPYALL
Copies all file information including:

  • File data and content
  • File attributes (read-only, hidden, system, etc.)
  • Timestamps (creation, modification, access dates)
  • Security information (NTFS ACLs)
  • Ownership information

🛡️ Error Handling

The script includes comprehensive error handling to ensure reliable operation and clear feedback about the copy process.

Exit Code Validation:

✓ Exit Code ≤ 3: Success
Indicates successful operation with possible informational messages

Success message displayed to user
✗ Exit Code > 3: Error
Indicates an error occurred during the copy operation

Error message with exit code displayed to user
Common Robocopy Exit Codes:

0 – No files were copied (no errors)
1 – All files copied successfully
2 – Extra files or directories detected
3 – Some files copied, some mismatches
4+ – Errors occurred during copy operation

💡 Usage Examples

⚠️ Important Prerequisite

Open PowerShell as Administrator from the Start menu before running these commands.

Example 1: Basic Syntax Template

General command structure for running the script:


powershell -ExecutionPolicy Bypass -File "[powershellscript]" -sourcePath "[sourcepath]" -destinationPath "[destinationpath]" -filetypes "*.docx"
Note: Replace the bracketed values with your actual paths and desired file types

Example 2: Copy DOCX Files

Copy only Microsoft Word documents from one directory to another:


powershell -ExecutionPolicy Bypass -File "C:\Scripts\copyfile_preservemetadata.ps1" -sourcePath "D:\word" -destinationPath "D:\backup\word" -filetypes "*.docx"
What this does: Copies all .docx files from D:\word to D:\backup\word while preserving all metadata

Example 3: Copy All PDF Files

Copy PDF documents with metadata preservation:


powershell -ExecutionPolicy Bypass -File "C:\Scripts\copyfile_preservemetadata.ps1" -sourcePath "\\server\documents\legal" -destinationPath "E:\archive\legal-docs" -filetypes "*.pdf"
What this does: Copies all PDF files from a network share to a local archive location

Example 4: Copy All File Types

Copy all files without filtering by type (omit the -filetypes parameter):


powershell -ExecutionPolicy Bypass -File "C:\Scripts\copyfile_preservemetadata.ps1" -sourcePath "C:\ProjectFiles" -destinationPath "D:\Backup\ProjectFiles"
What this does: Copies all files (*.* by default) from the source to destination directory

Why Use This Script?

Legal Compliance: Maintain original file timestamps critical for eDiscovery and legal proceedings

Complete Metadata: Preserve all file attributes, security settings, and ownership information

Selective Copying: Filter by file type to copy only the documents you need

Reliable Operation: Built on Windows robocopy command known for robust file handling

Easy to Use: Simple PowerShell command with clear parameter structure

Clear Feedback: Comprehensive error handling with informative success and error messages

💻 System Requirements
  • Windows operating system (Windows 7/Server 2008 or later)
  • PowerShell 3.0 or higher
  • Administrator privileges for running the script
  • Robocopy utility (included in Windows by default)
  • Sufficient disk space in destination location

Reviews

There are no reviews yet.

Be the first to review “Power Shell script that copies a file while preserving its metadata.”

Your email address will not be published. Required fields are marked *

Shopping Cart
  • Your cart is empty.