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\
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
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\
Create the destination folder first when it might not exist:
New-Item -ItemType Directory -Path .\backup -Force
Copy-Item -Path .\*.csv -Destination .\backup\
Copy folders: -Recurse
To copy a directory and everything inside it, add -Recurse:
Copy-Item -Path .\project -Destination .\project-backup -Recurse
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
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
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
For day-to-day work, a simple wildcard plus -Recurse is usually enough:
Copy-Item -Path .\docs\*.md -Destination .\site\content\ -Force
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
-
Confirm the destination exists (
Test-Path/New-Item -Force) before a bulk copy. - Use
-WhatIfto preview:
Copy-Item -Path .\*.log -Destination .\archive\ -WhatIf
- Prefer
-Recursefor folders — never assume an empty folder copy did what you wanted. - Use
-Forceonly when overwrite is intentional; backup first for critical files. - 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
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)