Powershell Out-Printer breaks Format-Table column

2019-07-23 05:06发布

Iam trying to use Format-Table to print some process data. I started with testing the output directly in the shell. Something along the lines of (Get-Process is just a substitute for my data):

Get-Process | Format-Table ProcessName, Id 

The problem is that the first column has some larger cells and i needed to set up some formatting like this:

$tableFormat = @{Expression={$_.ProcessName}; width=70}, @{Expression={$_.Handles}};
Get-Process | Format-Table $tableFormat 

In both cases, the output looked fine inside the shell. This is an example with $tableFormat:

DeviceDisplayObjectProvider                        240
dllhost                                            198
dwm                                                78
explorer                                           1077
explorer                                           599
fpassist                                           61
Idle                                               0
inetinfo                                           409
InetMgr                                            314
lsass                                              1132
lsm                                                167
mqsvc                                              235
msdtc                                              152
MsDtsSrvr                                          165
notepad++                                          249
Opc.Ua.ComServerWrapper                            526
Opc.Ua.DiscoveryServer                             530
OpcEnum                                            85
OPCSim                                             204
pageant                                            111
powershell                                         489
powershell_ise                                     442
PresentationFontCache                              151
procexp                                            183
PROCEXP64                                          373
ReportingServicesService                           693
services                                           302
smss                                               30
SMSvcHost                                          298
SMSvcHost                                          151

When i attempt to print the table to the default printer like this

...
Get-Process | Format-Table $tableFormat | lp

The result has broken columns (taken from a pdf printer, real output looks the same): printout

Other things I've tried so far:

  • Set the column alignment for all columns explicitly to left
  • autoSize(makes things worse)

This problem might be related to my previous question, when i still tried to manually format the table with tabs. See: Why does Powershell Out-Printer ignore `f (new page) in text?

1条回答
劫难
2楼-- · 2019-07-23 06:00

At first glance, this looks like a font problem. It may or may not be, but it's at least worth checking this out. In some fonts, all characters are the same width. In others, each character may have a unique width.

The console interface to powershell typically uses a fixed width font. The formatting you get from format-table may assume that all characters are the same width. If you print it on a printer with a variable width font, you'll get output like what you have shown us.

You may be able to check this out by somehow specifying to your printer that a fixed width font is to be used. "Courier" is one such font. I can't tell you how to do that in your case, because I don't know the particulars.

If my guess is on target, then specifying a fixed width font should make the columns look aligned again.

Another thing to check out is where the tab stops are set.

查看更多
登录 后发表回答