2

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
2
  • 1
    Santiago's answer is the preferable solution, but note that the only reason your (inefficient) approach didn't work is that you forgot to add -Stream to your Out-String call so as to request line-by-line output. Commented Apr 7, 2022 at 13:37
  • 1
    Thank you for the feedback! I am new to working with PowerShell so I learned a lot working on this issue. Commented Apr 7, 2022 at 13:39

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.