How to copy a folder and overwrite the full contents of the destination folder with powershell?

116 Views Asked by At

I want to copy folder 1 from the source directory to the destination directory however folder 1 is always a randomly generated number.

Source/Folder A:  
    folder 1 (random number)  
        file1.txt  
        file2.txt  

Destination/Folder A:  
    folder 1 (random number)  
        file1.txt  
        file2.txt  
        thisfile.txt  

Because it is randomly generated there is no way for me to go to the destination directory before and delete the contents of the folder.

I have tried to do this to compare and delete all of the items from the destination folder that are not in the source folder before I copy it however it seems to delete all of the files in the destination folder.

$A = "C:\source\folderA"
$B = "C:\destination\folderA"

Get-ChildItem $B -Recurse -File |Where-Object {! (Test-Path ("$A\$($_.Name)"))} |Remove-Item -force
0

There are 0 best solutions below