Functions/Repair-DashesInTextFile.ps1
function Repair-DashesInTextFile { <# .SYNOPSIS Repairs Unicode dash characters in the contents of a text file. .DESCRIPTION This cmdlet reads the contents of a specified text file and replaces Unicode dash characters (such as en dash – , em dash — , and minus sign −) with the standard ASCII hyphen (-). It then writes the updated content back to the same file. .PARAMETER FilePath The full path to the text file to be repaired. .PARAMETER WhatIf Simulates the operation without making any changes. .EXAMPLE Repair-DashesInTextFile -FilePath "C:\Docs\notes.txt" .EXAMPLE Repair-DashesInTextFile -FilePath "C:\Docs\notes.txt" -WhatIf .NOTES Author: Bruce Stump Module: PowerSysAdmin Version: 1.0.0.0 #> [CmdletBinding(SupportsShouldProcess = $true)] param ( [Parameter(Mandatory)] [string]$FilePath ) if (-not (Test-Path $FilePath)) { Write-Error "File not found: $FilePath" return } $unicodeDashes = @( [char]0x2013, # en dash – [char]0x2014, # em dash — [char]0x2212 # minus sign − ) try { $content = Get-Content -Path $FilePath -Raw $newContent = $content foreach ($dash in $unicodeDashes) { $newContent = $newContent -replace [regex]::Escape($dash), '-' } if ($newContent -ne $content) { if ($PSCmdlet.ShouldProcess($FilePath, "Replace Unicode dashes with ASCII hyphens")) { Set-Content -Path $FilePath -Value $newContent Write-Host "Updated: $FilePath" -ForegroundColor Green } } else { Write-Host "No changes needed: $FilePath" -ForegroundColor Gray } } catch { Write-Warning "Failed to process '$FilePath': $_" } } |