Functions/Repair-DashesInFileNames.ps1
function Repair-DashesInFileNames { <# .SYNOPSIS Repairs Unicode dash characters in file names by replacing them with standard ASCII hyphens. .DESCRIPTION This cmdlet scans the specified directory for files whose names contain Unicode dash characters such as en dashes (–), em dashes (—), or minus signs (−), and replaces them with the standard ASCII hyphen (-). This helps ensure compatibility across systems and tools that may not handle Unicode characters reliably in file names. .PARAMETER Path The directory path to scan for files. All files in this directory will be evaluated. .PARAMETER Recurse If specified, the cmdlet will scan all subdirectories recursively. .EXAMPLE Repair-DashesInFileNames -Path "C:\Documents" Scans the "Documents" folder and repairs any Unicode dash characters in file names. .EXAMPLE Repair-DashesInFileNames -Path "C:\Projects" -Recurse -WhatIf Recursively scans the "Projects" folder and shows what changes would be made, without renaming any files. .NOTES Author: Bruce Stump Module: PowerSysAdmin Version: 1.0.0.0 #> [CmdletBinding(SupportsShouldProcess = $true)] param ( [Parameter(Mandatory)] [string]$Path, [switch]$Recurse ) $unicodeDashes = @( [char]0x2013, # en dash – [char]0x2014, # em dash — [char]0x2212 # minus sign − ) $items = Get-ChildItem -Path $Path -File -Recurse:$Recurse foreach ($file in $items) { $originalName = $file.Name $newName = $originalName foreach ($dash in $unicodeDashes) { $newName = $newName -replace [regex]::Escape($dash), '-' } if ($newName -ne $originalName) { try { if ($PSCmdlet.ShouldProcess($file.FullName, "Rename to $newName")) { Rename-Item -Path $file.FullName -NewName $newName Write-Host "Renamed:`n $originalName → $newName" -ForegroundColor Green } } catch { Write-Warning "Failed to rename '$originalName': $_" } } } } |