How to Copy files or Folder without overwriting existing files?


To copy files/folders on the remote path without overwriting the existing files/folders, you can use multiple cmdlets like Copy-Item, Robocoy, and Xcopy, etc. As Copy-Item is a standard cmdlet, we will check if it's supported parameters can prevent overwriting.

If Copy-Item doesn’t work then we will check its alternate command. Copy-Item simply overwrites the files and folders on the destination path and the copies newer files.

For example, To copy files from the source folder C:\Test1 to the destination folder C:\Test2 below command is used and it simply overwrites the file without asking.

Example

Copy-Item C:\Test1\* C:\Test2 -Recurse -Verbose

Output

PS C:\Temp> Copy-Item C:\Test1\* C:\Test2 -Recurse -Verbose
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Test1\File1.txt
Destination: C:\Test2\File1.txt".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Test1\File2.txt
Destination: C:\Test2\File2.txt".

We have another -Confirm parameter, but it just confirms from the user that if files need to copy on the destination folder or not. If we choose $True for -Confirm parameter then it will ask for each file if it needs to copy or not but if you have hundreds of files on the system then this method won’t work

Copy-Item C:\Test1\* -Destination C:\Test2\ -Confirm:$true -Verbose
PS C:\Temp> Copy-Item C:\Test1\* -Destination C:\Test2\ -Confirm:$true -Verbose

Confirm
Are you sure you want to perform this action?
Performing the operation "Copy File" on target "Item: C:\Test1\File1.txt Destination:
C:\Test2\File1.txt".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):

With -Force parameter is for overwriting Read-only file content. So that is also not going to help so we can use another method like filtering existing files through Get-ChildItem and skipping them but that requires writing several codes and loop instead we have a robocopy command which supports preventing overwriting of files/ folder.

Robocopy C:\Test1\ C:\Test2\ /E /XC /XN /XO

Output

PS C:\> Robocopy C:\Test1\ C:\Test2\ /E /XC /XN /XO
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Saturday, August 29, 2020 2:15:33 PM
Source : C:\Test1\
Dest : C:\Test2\
Files : *.*
Options : *.* /S /E /DCOPY:DA /COPY:DAT /XO /XN /XC /R:1000000 /W:30
------------------------------------------------------------------------------
      2 C:\Test1\
   *EXTRA File    8 File3.txt
100% New File    11 File2.txt
------------------------------------------------------------------------------
         Total    Copied Skipped Mismatch FAILED Extras
Dirs :    1          0      1       0         0       0
Files :   2          1      1       0         0       1
Bytes :  22         11     11       0         0       8
Times : 0:00:00    0:00:00                0:00:00   0:00:00
Speed :    1833 Bytes/sec.
Speed :    0.104 MegaBytes/min.
Ended : Saturday, August 29, 2020 2:15:33 PM

Switches are explained below.

/E − Copies child items (like -recursive in Copy-Command)

/XC − Prevents overwriting the files which have the same timestamp.

/XN − Prevents overwriting of files with the newer timestamp than the source files.

/XO − Prevents overwriting of files with the older timestamp than the source files.

Updated on: 03-Sep-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements