Skip to content

Instantly share code, notes, and snippets.

@markwragg
Created June 30, 2017 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markwragg/31b81d15b308b6d72ccb61425522a76e to your computer and use it in GitHub Desktop.
Save markwragg/31b81d15b308b6d72ccb61425522a76e to your computer and use it in GitHub Desktop.
PowerShell function to remove spurious whitespace from scripts. Shared by JohnLBevan
function Remove-WhiteSpace {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string]$Path
,
[Parameter()]
[string]$Encoding = 'UTF8' #type FileSystemCmdletProviderEncoding is not public and does not match [System.Text.Encoding
)
process {
$tempFile = [System.IO.Path]::GetTempFileName()
$Path | Get-Content -Encoding $Encoding -Stream | %{"$_".TrimEnd()} | Set-Content -Path $tempFile -Encoding $Encoding
Move-Item -Path $tempFile -Destination $Path -Force
}
}
Get-ChildItem '.' -Recurse -File -Filter '*.ps1' | select -ExpandProperty fullname | Remove-WhiteSpace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment