The behaviour I would expect from the code below is:
Grab a list of the files in the source directory.
Loop through and copy each file to the backup destination, only if it does not already exist.
if (!(Test-Path C:\Folder\Destination)) { New-Item -ItemType Directory -Force -Path C:\Folder\Destination } $originalfiles = Get-ChildItem -Path "C:\Folder\Source" $originalfiles foreach ($file in $originalfiles) { Write-Host Write-Host File Name: -ForegroundColor DarkYellow Write-Host $file.Name Write-Host File Path: -ForegroundColor DarkYellow Write-Host $file.FullName $src = $file.FullName $dest = "C:\Folder\Destination\$($file.Name)" Copy-Item $src $dest }
I would have thought that the Copy-Item
cmdlet defaults to NOT overwrite, unless you specify the -Force
flag. This is the behaviour I have seen in the past when I originally encountered situations where I did want to overwrite.
Also, I thought it may be the introduction of the foreach
loop but I tried the copy command, on it's own, with hardcoded paths for a single file, and it is still the same.
Should I restart my IDE, or is it a mistake I have overlooked?