Copy-Item and Move-Item help you copy or move files and folders easily. They save time by automating these tasks.
Copy-Item and Move-Item in 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.
Copy-Item -Path C:\Docs\file.txt -Destination C:\Backup\file.txt
Copy-Item -Path C:\Photos -Destination D:\BackupPhotos -Recurse
Move-Item -Path C:\Temp\oldfile.txt -Destination C:\Archive\oldfile.txt
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.
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
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.
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.