Copy Item - PowerShell


Overview

Copy-Item copies one or more files or folders to a destination location. It can handle copying files across different drives, folders, or even networks.

Syntax

Copy-Item [-Path] <String[]> [-Destination] <String>
[-Recurse] [-Force] [-Verbose]
[-Confirm] [-WhatIf] [-Include] <String[]> [-Exclude] <String[]>
[-PassThru] [-File] [-Directory] [-Container] [-Link]
[-Content] [-SkipCRC] [-SkipFileTimeCheck]
[-Credential <PSCredential>]

Options/Flags

  • -Path <String[]>: Specifies the file(s) or folder(s) to copy.
  • -Destination : Specifies the destination path where the files should be copied.
  • -Recurse: Copies files and subfolders within the specified source path(s) recursively.
  • -Force: Overwrites existing files in the destination without prompting for confirmation.
  • -Verbose: Displays detailed progress information about the copy operation.
  • -Confirm: Prompts for confirmation before overwriting existing files or creating folders.
  • -WhatIf: Simulates the copy operation without actually copying anything.
  • -Include <String[]>: Specifies a filter to include only files matching the pattern(s).
  • -Exclude <String[]>: Specifies a filter to exclude files matching the pattern(s).
  • -PassThru: Returns the copied item(s) as an object array.
  • -File: Copies only files.
  • -Directory: Copies only directories.
  • -Container: Copies only containers (files and directories).
  • -Link: Creates hard links instead of copies (Windows only).
  • -Content: Copies only the data/content of the items (without permissions or timestamps).
  • -SkipCRC: Skips comparing the CRC (checksum) of the source and destination files.
  • -SkipFileTimeCheck: Skips comparing the file timestamps of the source and destination files.
  • -Credential : Specifies credentials for network or remote file access.

Examples

Simple file copy:

Copy-Item "sourcefile.txt" "destinationpath\newfile.txt"

Recursive folder copy:

Copy-Item -Path "sourcefolder" -Destination "targetfolder" -Recurse

Filter and exclude files:

Copy-Item -Path "*.txt" -Destination "backupfolder" -Include "*.log" -Exclude "*.temp"

Copy file only, excluding subfolders:

Copy-Item -Path "sourcefolder" -Destination "targetfolder" -File -Exclude "*/*"

Common Issues

  • Path not found: Ensure that the specified source and destination paths exist and are valid.
  • Access denied: Check if you have sufficient permissions to access both the source and destination locations.
  • File already exists: If using -Confirm, manually confirm file replacements or use -Force to overwrite without prompting.
  • Invalid parameters: Verify that all parameter values (e.g., paths, filters) are valid and properly formatted.

Integration

Copy and rename simultaneously:

Copy-Item -Path "sourcefile.txt" -Destination "targetfolder\renamedfile.txt" -PassThru | Rename-Item -NewName "newname.txt"

Copy and compress in one step:

Copy-Item -Path "sourcefolder" -Destination "compressed.zip" -Compress
  • Move-Item: Moves files or folders to a new location.
  • New-Item: Creates new files or folders.
  • Get-Item: Retrieves information about files or folders.
  • For more information, see: Microsoft Docs: Copy-Item