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

Powershell

Powershell Problem Overview


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?

Powershell Solutions


Solution 1 - Powershell

It has a -force parameter.​​​​

Solution 2 - Powershell

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsivabudhView Question on Stackoverflow
Solution 1 - PowershellrerunView Answer on Stackoverflow
Solution 2 - Powershelli_am_jorfView Answer on Stackoverflow