[powershell] 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

This question is related to powershell

The answer is


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.


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"