I am trying to copy folder and its sub folder which has some files in it.
For example my folder1
contains folders like abc
, xyx
and again inside this folder I have files.
I want to copy the full folder such as folder1
and paste it in the destination, if it exists then replace all the files in it. I am using the below commands.
My problem is if I run this script multiple times it is creating subfolders inside subfolder, like inside abc
it is again creating abc
which I do not want.
$sourceFolder='C:\abc\foder1'
$destFolder='c:\Test\folder1'
Get-ChildItem $sourceFolder | ForEach-Object `
{
$destFullPath = Join-Path $destFolder ($_.BaseName + $_.Extension)
Copy-Item -Path $_.FullName -Destination $destFullPath -Force -Recurse
}