简体   繁体   中英

Powershell remove trailing spaces in powershell output

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM