I currently have a script that pings servers and checks the status of services running on each server.
I save the output with Out-File but PowerShell places ellipses or "..." after long strings. I don't want it to do this. For example:
MachineName ServiceName Status StartType
----------- ----------- ------ ---------
SrvGtw01 Test.MyService.... Running
I want it to display the full name like:
MachineName ServiceName Status StartType
----------- ----------- ------ ---------
SrvGtw01 Test.MyServiceName.Here Stopped Disabled
I've been reading that you can set the $FormatEnumerationLimit
preference variable to -1
and I have tried that but it's not working. I'm not sure how I should place it in my script.
The Format-Table cmdlet formats the output of a command as a table with the selected properties of the object in each column. The object type determines the default layout and properties that are displayed in each column. You can use the Property parameter to select the properties that you want to display.
The Format-List cmdlet formats the output of a command as a list of properties in which each property is displayed on a separate line. You can use Format-List to format and display all or selected properties of an object as a list ( Format-List -Property * ).
Summary. The $FormatEnumerationLimit variable is a neat feature of PowerShell that allows you to see more occurrences when using Format-Table . But remember: if you are using this variable in a function or a script, you should be aware of the scoping issue.
The $FormatEnumerationLimit
preference variable doesn't apply here, because its purpose is to determine how many elements of a collection-valued property to display (e.g, $FormatEnumerationLimit = 2; [pscustomobject] @{ prop = 1, 2, 3 }
prints (at most) 2 elements from .prop
's value and hints at the existence of more with ...
; e.g., {1, 2...}
).
Instead, you must:
(a) ensure that individual columns don't truncate their values on display:
Format-Table -Autosize
first.and (b) ensure that the overall output width can fit all columns:
Pipe to Out-File -Width
with a sufficiently large value (don't use [int]::MaxValue
, though, because every line of tabular output gets padded to that very width[1])
.
Caveat: If you don't set -Width
explicitly - as would happen if you just used >
, for instance - the current console window's width is used - whatever it happens to be.
For instance:
# Assumes that the objects in $results only contain the properties
# of interest (MachineName, ServiceName, Status, StartType); you
# can also pass an explicit list of output properties to Format-Table, however.
$results | Format-Table -AutoSize | Out-File -Width 512 C:\log.txt -Append
Note: To preview the output in the console - which may involve line-wrapping - useOut-String -Width 512
instead.
[1] In PowerShell Core this undesirable last-column padding has been removed, as of at least v6.1.0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With