Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell 'PowerShell' Copy-Item to unconditionally copy files

Tags:

powershell

The following PowerShell script works if the resources are not there.

  Copy-Item $src_dir $dst_dir$agent_folder -recurse 

But if the resources are there, it will say:

+   Copy-Item <<<<  $src_dir $dst_dir$agent_folder -recurse     + CategoryInfo          : ResourceExists: (C:\Users\Pac\Desktop\Agents\Agent0\lib:S    tring) [Copy-Item], IOException     + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.CopyItemComm    and 

What do I have to add to the command so that it will unconditionally copy the files?

like image 315
sivabudh Avatar asked Nov 24 '09 04:11

sivabudh


People also ask

How do I copy only the latest or updated files in PowerShell?

To copy only updated or newer files with PowerShell, we can use Copy-Item with some logic in the script that to check if the files exist on the destination folder if not then copy that file and if yes then compare the timestamp and copy the latest file.

What is the PowerShell command including parameters to copy that file?

The Copy-Item cmdlet copies an item from one location to another location in the same namespace. For instance, it can copy a file to a folder, but it can't copy a file to a certificate drive. This cmdlet doesn't cut or delete the items being copied.

How do I copy a file from one directory to another in PowerShell?

Use Copy-Item Command to Copy Files Recursively in PowerShell. We usually run into situations where we have many subfolders in the parent folder, which files we'd like to copy over. Using the -Recurse parameter on Copy-Item will look in each subfolder and copy all files and folders in each recursively.


2 Answers

It has a -force parameter.​​​​

like image 164
rerun Avatar answered Sep 23 '22 20:09

rerun


From the documentation (help copy-item -full):

-force <SwitchParameter>     Allows cmdlet to override restrictions such as renaming existing files as long as security is not compromised.      Required?                    false     Position?                    named     Default value                False     Accept pipeline input?       false     Accept wildcard characters?  false 
like image 33
i_am_jorf Avatar answered Sep 20 '22 20:09

i_am_jorf