DEV Community

Cover image for PowerShell Copy-Item: Duplicate Files Fast and Safely
arnostorg
arnostorg

Posted on

PowerShell Copy-Item: Duplicate Files Fast and Safely

The PowerShell Copy-Item cmdlet duplicates files and folders — the modern replacement for classic CMD copy and xcopy. When you need a backup of a config, to stage assets into a deploy folder, or to clone a directory tree with filters, Copy-Item is clearer and safer than typing long xcopy switches.

For the official reference, see Microsoft Learn’s Copy-Item.

Want to practice PowerShell in a live browser terminal (no install)? Try the free interactive lessons on CMD Master — PowerShell and command-line training.

What does Copy-Item do?

Copy-Item -Path .\report.txt -Destination .\backup\
Enter fullscreen mode Exit fullscreen mode

That copies report.txt into the backup folder. If the destination is a folder that already exists, the file keeps its name. If the destination includes a new file name, PowerShell renames on copy:

Copy-Item -Path .\report.txt -Destination .\backup\report-2026-09-21.txt
Enter fullscreen mode Exit fullscreen mode

Copy one file or many

Point -Path at a single file, a wildcard, or several paths:

Copy-Item -Path .\notes.txt -Destination .\archive\
Copy-Item -Path .\*.log -Destination .\logs-backup\
Copy-Item -Path .\a.txt,.\b.txt -Destination .\out\
Enter fullscreen mode Exit fullscreen mode

Create the destination folder first when it might not exist:

New-Item -ItemType Directory -Path .\backup -Force
Copy-Item -Path .\*.csv -Destination .\backup\
Enter fullscreen mode Exit fullscreen mode

Copy folders: -Recurse

To copy a directory and everything inside it, add -Recurse:

Copy-Item -Path .\project -Destination .\project-backup -Recurse
Enter fullscreen mode Exit fullscreen mode

Without -Recurse, PowerShell can create an empty destination folder (or fail, depending on version and path shape) — always use -Recurse when you mean “copy the tree.”

Overwrite existing files: -Force

By default, Copy-Item may refuse to overwrite a read-only or existing file. Use -Force when you intentionally want to replace:

Copy-Item -Path .\app.config -Destination .\deploy\app.config -Force
Enter fullscreen mode Exit fullscreen mode

Be deliberate: -Force skips the “are you sure?” friction. Prefer an explicit backup first when the destination is production config.

Filter while you copy

Combine wildcards with -Include / -Exclude (often clearest when you pipe from Get-ChildItem):

# copy only .ps1 scripts from a tree
Get-ChildItem -Path .\src -Filter *.ps1 -Recurse |
  Copy-Item -Destination .\scripts-backup\ -Force
Enter fullscreen mode Exit fullscreen mode

Or exclude junk folders by filtering upstream:

Get-ChildItem -Path .\app -Recurse -File |
  Where-Object { $_.FullName -notmatch '\\node_modules\\|\\.git\\' } |
  Copy-Item -Destination {
    Join-Path .\clean-copy ($_.FullName.Substring((Resolve-Path .\app).Path.Length + 1))
  } -Force
Enter fullscreen mode Exit fullscreen mode

For day-to-day work, a simple wildcard plus -Recurse is usually enough:

Copy-Item -Path .\docs\*.md -Destination .\site\content\ -Force
Enter fullscreen mode Exit fullscreen mode

Keep the original timestamps (when you care)

Copy-Item updates the new file’s timestamps to “now.” If you need a byte-for-byte archival copy that preserves metadata, reach for robocopy or a dedicated sync tool. For scripts and deploy staging, the default timestamps are fine.

Copy-Item vs copy vs xcopy vs robocopy

Tool Best for
Copy-Item PowerShell scripts, pipelines, clear Verb-Noun readability
copy Tiny one-file CMD jobs
xcopy Older batch trees with /E / /I habits
robocopy Large mirrors, retries, multi-thread, detailed logs

Prefer Copy-Item when you are already in PowerShell. Prefer robocopy for huge folder mirrors or scheduled backups.

Safe habits

  1. Confirm the destination exists (Test-Path / New-Item -Force) before a bulk copy.
  2. Use -WhatIf to preview:
Copy-Item -Path .\*.log -Destination .\archive\ -WhatIf
Enter fullscreen mode Exit fullscreen mode
  1. Prefer -Recurse for folders — never assume an empty folder copy did what you wanted.
  2. Use -Force only when overwrite is intentional; backup first for critical files.
  3. Avoid copying node_modules, .git, or build caches unless you truly need them.

Practice it live

Open a free PowerShell lesson on CMD Master’s interactive CLI platform and try Copy-Item in a real terminal in your browser — no VM, no install.

Quick cheat sheet

Copy-Item -Path .\file.txt -Destination .\backup\
Copy-Item -Path .\file.txt -Destination .\backup\renamed.txt
Copy-Item -Path .\folder -Destination .\folder-copy -Recurse
Copy-Item -Path .\*.log -Destination .\logs\ -Force
Copy-Item -Path .\src\*.ps1 -Destination .\out\ -WhatIf
Enter fullscreen mode Exit fullscreen mode

Once this clicks, pair it with Get-ChildItem for discovery and Remove-Item when you clean up after a copy. That trio covers most day-to-day file moves on Windows.

Top comments (0)