3. File System Management Tasks - Powershell

3.4 Copying Files and Folders

to copy a file from one path to another, use the Copy-Item cmdlet.

Copy-Item -Path C:\Users\xle0x\test.txt -Destination C:\temp\test.txt

if the target file already exists, the copy attempt will fail. to overwrite the target file, use the -Force parameter.

Copy-Item -Path `C:\Users\xle0x\test.txt` -Destination `C:\temp\test.txt` -Force

Copy from local to remote or vice versa

Copy from remote to local

to copy files

Copy-Item -Path \\server\share$\test.txt -Destination C:\temp\test.txt

to copy folders

Copy-Item -Path \\server\share$\test -Destination C:\temp\test -Recurse

Copy from local to remote

to copy files

Copy-Item -Path C:\temp\test.txt -Destination \\server\share$\test.txt

to copy folders

Copy-Item -Path C:\temp\test -Destination \\server\share$\test -Recurse

Copy from remote to remote

to copy files

Copy-Item -Path \\server1\share$\test.txt -Destination \\server2\share$\test.txt

to copy folders

Copy-Item -Path \\server1\share$\test -Destination \\server2\share$\test -Recurse

Copy specific files

if you want to only copy all .txt files, you can use the -Filter parameter.

Copy-Item -Filter *.txt -Path C:\Users\xle0x\Documents  -Recurse -Destination C:\Users\xle0x\Backup-Docs

this will copy all .txt files from the C:\Users\xle0x\Documents directory to the C:\Users\xle0x\Backup-Docs directory.