You are on page 1of 1

$moveAllToTLD = {

param(
$Path, $TLD = $Path
)
$i = 1
Get-ChildItem -Path $Path -File -Recurse | ForEach-Object {
$name = $_.Name
Write-Verbose "_Name- '${name}'." -Verbose
$destinationFile = Join-Path $TLD $_.Name
Write-Verbose "Destination '${destinationFile}'." -Verbose
if (Test-Path -LiteralPath $destinationFile) {
$destinationFile = Join-Path $TLD ($_.BaseName + "_$i" +
$_.Extension)
Write-Verbose "Rename to '${destinationFile}'." -Verbose
$i+=1
}
$_ | Move-Item -Destination $destinationFile
}
& $tailRecursion -Path $Path
}

& $moveAllToTLD -Path 'D:\test'

adapted from:
https://stackoverflow.com/questions/16839787/powershell-move-item-rename-if-file-
exists
https://stackoverflow.com/questions/52087244/copy-files-renaming-if-already-exists-
powershell

You might also like