Home > database >  Powershell remove trailing spaces in powershell output
Powershell remove trailing spaces in powershell output

Time:04-08

I am trying to format the output of my command for usage on another machine. I use the trim() function but still PowerShell insists on making the strings equal length.

I think it has to do with me setting the size of the string but I had to do this to avoid the paths being truncated. How can I circumvent this behavior?

This is my command:

Get-ChildItem -Recurse .\x\ -File | Get-FileHash -Algorithm MD5 | Select-Object @{Name = 'Hash'; Expression = {$_.Hash.ToLower()}}, @{Name = 'Path'; Expression = { Resolve-Path -Path $_.Path -Relative }} | Format-Table -HideTableHeaders -AutoSize | Out-String -Width 10000 | ForEach-Object {  $_.Trim().replace('\', '/').trimend()} > output.md5

this is the output:

11ddbaf3386aea1f2974eee984542152 ./x/ffff.txt                                                                                                     
76e2711e46c3c0ea96007f251c34b43a ./x/superlongfoldernameidkwhattoputherebutitisgoingtobelong/anextreeeeemelylongdocumentnanemthatisprobablycut.txt

CodePudding user response:

Don't use Format-Table and Out-String -Width instead use a loop to construct an array of strings:

Get-ChildItem -Recurse .\x\ -File | Get-FileHash -Algorithm MD5 | ForEach-Object {
    '{0} {1}' -f $_.Hash.ToLower(), (Resolve-Path $_.Path -Relative).Replace('\', '/')
} | Set-Content output.md5
  • Related