$root = (Get-Location).Path
Get-ChildItem -Path $root -Recurse -File | Where-Object { $_.DirectoryName -ne $root } | ForEach-Object {
$targetPath = Join-Path $root $_.Name
$baseName = $_.BaseName
$ext = $_.Extension
$counter = 1
# Если файл с таким именем уже есть, подбираем новое имя
while (Test-Path -LiteralPath $targetPath) {
$newName = "{0}_{1}{2}" -f $baseName, $counter, $ext
$targetPath = Join-Path $root $newName
$counter++
}
Move-Item -LiteralPath $_.FullName -Destination $targetPath
}
# Удаляем оставшиеся пустые подпапки
Get-ChildItem -Path $root -Directory | Remove-Item -Recurse -Force