Using Powershell, I want to force a copy of the folder / files without deleting the extra files in the existing destination folder:

I am using Powershell and trying to force a copy of the folder / files without deleting the extra files in the existing destination folders. I'm stuck trying to get a working team.

Below is my code, any suggestions to fix it?

Copy-Item -Force -Recurse โ€“Verbose $releaseDirectory -Destination $sitePath 
+4
source share
2 answers

you need to be sure that

 $realeseDirectory 

- it's something like

 c:\releasedirectory\* 

Copy-item will never delete extra files or folders as intended, but with -force it will owerwrite if the file already exists

+2
source

Your question is not very clear. Thus, you may have to adjust the function a bit. By the way, if you are trying to deploy a website, copying a directory is not the best way.

 function Copy-Directory { param ( [parameter(Mandatory = $true)] [string] $source, [parameter(Mandatory = $true)] [string] $destination ) try { Get-ChildItem -Path $source -Recurse -Force | Where-Object { $_.psIsContainer } | ForEach-Object { $_.FullName -replace [regex]::Escape($source), $destination } | ForEach-Object { $null = New-Item -ItemType Container -Path $_ } Get-ChildItem -Path $source -Recurse -Force | Where-Object { -not $_.psIsContainer } | Copy-Item -Force -Destination { $_.FullName -replace [regex]::Escape($source), $destination } } catch { Write-Error "$($MyInvocation.InvocationName): $_" } } $releaseDirectory = $BuildFilePath + $ProjectName + "\" + $ProjectName + "\bin\" + $compileMode + "_PublishedWebsites\" + $ProjectName $sitePath = "\\$strSvr\c$\Shared\WebSites" Copy-Directory $releaseDirectory $sitePath 
+1
source

All Articles