DEV Community

Cover image for Copy Files in PowerShell: Copy-Item with Safety
arnostorg
arnostorg

Posted on

Copy Files in PowerShell: Copy-Item with Safety

Copy Files in PowerShell: Copy-Item with Safety

Duplicating files is one of the most common tasks. Learn to copy safely.

How It Works

Copy-Item creates a duplicate of a file or folder. The original stays, and you get a copy. You control where the copy goes and what to name it.

Always copy first before moving important filesβ€”it's your safety net.

Code Examples

Copy Single File

# Copy file.txt to backup.txt (both in same folder)
Copy-Item file.txt backup.txt

# Result: You now have file.txt AND backup.txt
Enter fullscreen mode Exit fullscreen mode

Copy to Different Folder

# Copy file to backup folder
Copy-Item report.txt C:\backup\report.txt

# Result: Original stays in current folder, copy is in backup folder
Enter fullscreen mode Exit fullscreen mode

Copy Multiple Files

# Copy all .txt files to archive folder
Copy-Item *.txt C:\archive\

# All text files are copied, originals stay
Enter fullscreen mode Exit fullscreen mode

Copy Entire Folder Structure

# Copy folder and everything inside it
Copy-Item C:\MyProject C:\MyProject_Backup -Recurse

# -Recurse copies subfolders and all their files
Enter fullscreen mode Exit fullscreen mode

Most Used Options

  • -Path - The file or folder to copy
  • -Destination - Where to copy it to
  • -Recurse - Copy folders and everything inside
  • -Force - Overwrite if destination already exists

The Trick: Power Usage

The safe backup pattern:

# Before moving important files, copy them first
Copy-Item "important.xlsx" "backup\important.xlsx"

# Verify the copy worked
Get-ChildItem backup\important.xlsx

# Now the original is safe! You can move it knowing you have a backup
Enter fullscreen mode Exit fullscreen mode

Copy with logging:

# See what gets copied
Copy-Item *.txt C:\archive\ -Verbose
# Shows each file as it copies
Enter fullscreen mode Exit fullscreen mode

Learn It Through Practice

Stop reading and start practicing:

πŸ‘‰ Practice on your browser

The interactive environment lets you type these commands and see real results.

Part of PowerShell for Beginners

This is part of the PowerShell for Beginners series:

  1. Getting Started - Your first commands
  2. Command Discovery - Find what exists
  3. Getting Help - Understand commands
  4. Working with Files - Copy, move, delete
  5. Filtering Data - Where-Object and Select-Object
  6. Pipelines - Chain commands together

Related Resources

Summary

You now understand:

  • How this command works
  • The most useful options
  • One powerful trick
  • Where to practice hands-on

Practice these examples until they're automatic. Mastery comes from repetition.


Practice now: Head to the interactive environment and try these commands yourself. That's how PowerShell clicks for you!

What would you like to master next?

Top comments (1)

Collapse
 
scsoi profile image
疏影 •

Good safety checklist around Copy-Item. The next-level concern is the source/destination itself: once your SMB share is mounted as a drive letter, Copy-Item sees a normal filesystem path and the safety tips all apply the same way. ScsDriver handles SMB alongside S3 / SFTP / WebDAV, so the same mount client can target a Windows share today and a S3 bucket tomorrow β€” Copy-Item doesn't care, which is the point. Auto-reconnect across sleep / VPN / η½‘ε‘εˆ‡ζ’ means the user's drive letter survives their laptop state changes without the script author having to retry the copy themselves.

Couple of small notes from running Copy-Item across mount clients:

  • -Force will silently overwrite read-only files on most mount clients; that matches the 'safety' framing in this post, but it's worth being explicit.
  • Get-Item -Stream * for NTFS alternate data streams doesn't work through any cloud-backed mount client β€” fall back to a local copy first if you really need ADSes.
  • For cross-backend copies (SMB β†’ S3 β†’ SMB), keeping the source as a drive letter (ScsDriver) means the script is the same shape regardless of which backend the team standardized on; only the mount table changes. Thanks for the writeup.