0
0
PowerShellscripting~5 mins

Copy-Item and Move-Item in PowerShell

Choose your learning style9 modes available
Introduction

Copy-Item and Move-Item help you copy or move files and folders easily. They save time by automating these tasks.

You want to back up important files to another folder.
You need to organize files by moving them to different folders.
You want to duplicate a file before editing it.
You want to clean up a folder by moving files elsewhere.
You want to copy files from one computer to another using a script.
Syntax
PowerShell
Copy-Item -Path <source> -Destination <target> [-Recurse]
Move-Item -Path <source> -Destination <target>

-Path is the file or folder you want to copy or move.

-Destination is where you want the file or folder to go.

Examples
Copies a single file to the Backup folder.
PowerShell
Copy-Item -Path C:\Docs\file.txt -Destination C:\Backup\file.txt
Copies the entire Photos folder and its contents to BackupPhotos.
PowerShell
Copy-Item -Path C:\Photos -Destination D:\BackupPhotos -Recurse
Moves a file from Temp to Archive folder.
PowerShell
Move-Item -Path C:\Temp\oldfile.txt -Destination C:\Archive\oldfile.txt
Sample Program

This script shows the files in TestFolder, copies file1.txt to a new file, then moves that new file to a different name. It prints the folder contents before and after each step.

PowerShell
Write-Output "Before copy:"
Get-ChildItem -Path .\TestFolder

Copy-Item -Path .\TestFolder\file1.txt -Destination .\TestFolder\CopyOfFile1.txt

Write-Output "After copy:"
Get-ChildItem -Path .\TestFolder

Move-Item -Path .\TestFolder\CopyOfFile1.txt -Destination .\TestFolder\MovedFile1.txt

Write-Output "After move:"
Get-ChildItem -Path .\TestFolder
OutputSuccess
Important Notes

Use -Recurse with Copy-Item to copy folders and all their contents.

Move-Item deletes the original file after moving it.

If the destination file exists, Copy-Item and Move-Item will overwrite it without prompting by default. Use -Force to ensure overwriting without errors.

Summary

Copy-Item duplicates files or folders to a new location.

Move-Item moves files or folders, removing them from the original place.

Both commands help automate file management tasks quickly and safely.