Skip to main content

Copy individual files and rename duplicates with PowerShell.

#############################################################################
# How to Copy Individual Files and Rename Duplicates with Powershell
# http://www.adminarsenal.com/admin-arsenal-blog/copy-individual-files-and-rename-duplicates/
#############################################################################

$sourceFile = "c:\temp\file.txt"
$destinationFile = "c:\temp\nonexistentdirectory\file.txt"

if (Test-Path $destinationFile)
{
    $i = 0
    while (Test-Path $destinationFile)
    {
        $i += 1
        $destinationFile = "c:\temp\nonexistentdirectory\file$i.txt"
    }
}
else
{
    New-Item -ItemType File -Path $destinationFile -Force
}

Copy-Item -Path $sourceFile -Destination $destinationFile -Force