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 (0)