Andy Walker
Andy Walker

Reputation: 385

Powershell output

I'm having a little issue with controlling output in ps. Here's the line of code in question;

$result|sort-object cn  | format-table -wrap -autosize 

If I append

| out-file $logfile

the last column of my output is truncated. Now I know that if I changed the width of my console session and run the script again, my output is fine, but there must be a better way of doing this? I also attempted to use add-content but I think I must be missing something as the expression isn't being evalulated correctly and I just get a series of references to system-object in my logfile.

Upvotes: 2

Views: 1742

Answers (2)

Glenn
Glenn

Reputation: 1855

The best way I've determined so far is to use Out-String with a -Width longer than you expect the entire line to be:

$result | Format-Table -Autosize | Out-String -Width 4096

The only problem with the above is that it will pad the entire line with spaces. In order to get around that, add the -Stream switch and .Trim() each line:

$result | Format-Table -Autosize | Out-String -Width 4096 -Stream | %{ $_.Trim() }

This is also nice for piping the results to the clipboard with clip.exe (if I don't have the PSCX module installed with the Out-Clipboard command):

$result | Format-Table -Autosize | Out-String -Width 4096 -Stream | %{ $_.Trim() } | clip.exe

Upvotes: 1

Andy Schneider
Andy Schneider

Reputation: 8684

You can use the -width parameter for the out-file cmdlet. You might try out-file -width 500 so nothing gets truncated.

Upvotes: 4

Related Questions