srk786
srk786

Reputation: 549

How to overwrite files with Copy-Item in PowerShell

I am trying to copy content of a folder, but there are two files which I would like to exclude. The rest of all the content should be copied to a new location and existing content on that new location should be overwritten.

This is my script. It works fine if my destination folder is empty, but if I have files and folder, it doesn't overwrite them.

$copyAdmin = $unzipAdmin + "/Content/*"
$exclude = @('Web.config','Deploy')
Copy-Item  -Path $copyAdmin -Destination $AdminPath -Exclude $exclude -Recurse -force

Upvotes: 28

Views: 172877

Answers (3)

NoOne
NoOne

Reputation: 4091

How about calling the .NET Framework methods?

You can do ANYTHING with them... :

[System.IO.File]::Copy($src, $dest, $true);

The $true argument makes it overwrite.

Upvotes: 5

clD
clD

Reputation: 2611

Robocopy is designed for reliable copying with many copy options, file selection restart, etc.

/xf to excludes files and /e for subdirectories:

robocopy $copyAdmin $AdminPath /e /xf "web.config" "Deploy"

Upvotes: 7

Daniel Lindegaard
Daniel Lindegaard

Reputation: 611

As I understand Copy-Item -Exclude then you are doing it correct. What I usually do, get 1'st, and then do after, so what about using Get-Item as in

Get-Item -Path $copyAdmin -Exclude $exclude |
Copy-Item  -Path $copyAdmin -Destination $AdminPath -Recurse -force

Upvotes: 30

Related Questions