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\Alkane1.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