Skip to content

Copy a File Without Overwriting an Existing Copy in Windows

Copy a File Without Overwriting an Existing Copy in Windows

If the destination may already contain a file with the same name, decide whether you want to skip, version or rename the incoming copy. A simple PowerShell pattern can create a unique name while preserving the original extension.

$Source = 'C:\Source\report.csv'
$DestinationFolder = 'C:\Archive'
$name = [IO.Path]::GetFileNameWithoutExtension($Source)
$ext  = [IO.Path]::GetExtension($Source)
$target = Join-Path $DestinationFolder ($name + $ext)
$i = 1

while (Test-Path -LiteralPath $target) {
    $target = Join-Path $DestinationFolder ("{0}-{1}{2}" -f $name,$i,$ext)
    $i++
}

Copy-Item -LiteralPath $Source -Destination $target

This produces names such as report-1.csv, report-2.csv and so on.

For backup workflows

If uniqueness must survive concurrent jobs, use a timestamp or GUID and verify the copy/hash afterwards. For large directory trees, use a purpose-built backup/synchronisation tool rather than a loop that silently creates thousands of duplicates.