[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [switch]$Recurse, [switch]$Apply ) $ErrorActionPreference = 'Stop' if (-not (Test-Path -LiteralPath $Path -PathType Container)) { throw "Folder not found: $Path" } $root = (Resolve-Path -LiteralPath $Path).Path if ($Recurse) { $items = @(Get-ChildItem -LiteralPath $root -File -Recurse -ErrorAction SilentlyContinue) } else { $items = @(Get-ChildItem -LiteralPath $root -File -ErrorAction SilentlyContinue) } $planned = 0 foreach ($file in $items) { $extension = [System.IO.Path]::GetExtension($file.Name) if ([string]::IsNullOrEmpty($extension)) { continue } $lower = $extension.ToLowerInvariant() if ($extension -ceq $lower) { continue } $baseName = $file.Name.Substring(0, $file.Name.Length - $extension.Length) $newName = $baseName + $lower $target = Join-Path -Path $file.DirectoryName -ChildPath $newName if (Test-Path -LiteralPath $target) { $existing = Get-Item -LiteralPath $target -ErrorAction SilentlyContinue if (($null -eq $existing) -or ($existing.FullName -ne $file.FullName)) { Write-Warning "Skip, target exists: $target" continue } } if ($Apply) { Rename-Item -LiteralPath $file.FullName -NewName $newName -ErrorAction Stop Write-Host "Renamed: $($file.Name) -> $newName" } else { Write-Host "Preview: $($file.Name) -> $newName" } $planned++ } if (-not $Apply) { Write-Host "Preview: $planned file(s). Add -Apply to rename." }