Kae Travis

Fix PSScriptAnalyzer Line has Trailing Whitespace

This blog provides a PowerShell script to fix PSScriptAnalyzer line has trailing whitespace warnings.

Given the path to a PowerShell module it will iterate through any psd1 and psm1 files and remove any trailing white space:

$pathToModule = "C:\Program Files\WindowsPowerShell\Modules\Alkane\1.0.0"
$fileExtensions = @(".psd1",".psm1")

get-childitem -Path $pathToModule | where {$_.extension -in $fileExtensions} | foreach {

    $filePath = $_.FullName

    #read all lines from the file
    $fileContent = Get-Content -Path $filePath

    #remove trailing spaces from each line
    $updatedContent = $fileContent | ForEach-Object { $_.TrimEnd() }

    #save the updated content back to the file
    Set-Content -Path $filePath -Value $updatedContent

    write-host "Trailing spaces removed from file: $filePath"

}
Fix PSScriptAnalyzer Line has Trailing Whitespace
Fix PSScriptAnalyzer Line has Trailing Whitespace

Leave a Reply