This one liner trims trailing whitespace from every file in the current directory.
dir -rec -file | % { $t = $_ | gc | % { $_.TrimEnd() }; sc $_.FullName $t; }
Annotated Version: Note that in the outer loop $_
refers to the current file and in the inner loop $_
refers to the current line of text.
# loop thru each file
dir -Recurse -File | ForEach-Object
{
# for each file, loop thru each line of text and trim each one
$trimmed = $_ | Get-Content | ForEach-Object
{
$_.TrimEnd()
};
# write the trimmed text back to the file
Set-Content $_.FullName $trimmed;
}
Removing all trailing white-space in one fell swoop can be helpful when working with legacy code.