Search code examples
powershellformattext-files

How to increase spacing between columns in txt file using Powershell commands?


I want to increase the spacing between the columns in my text file, as the columns are too close together and it is not easy to read the data.

A section of how my text file looks like now:

This is my powershell code:

$output = Get-ADUser -Filter "SamAccountName -eq 'jared.h'" -Properties whenChanged,DistinguishedName | Select SamAccountName,Enabled,whenChanged,DistinguishedName

$output | Format-Table * | Out-File -FilePath C:\output.txt -Width 5000

What do I need to change to get an output that looks like this?

enter image description here


I am unable to use Format-Table -AutoSize as I need to show all 13 columns in the text file.

I have also tried adding a property width to the format-table:

$output | Format-Table -Property @{ e='SamAccountName,Enabled,whenChanged'; width = 1000 } | Out-File -FilePath C:\output.txt -Width 5000 Format-Table 

But it removes all the data, and takes away any spacing between the headers and replaces them with a comma:

SamAccountName,Enabled,whenChanged 
----------------------------------                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Solution

  • You will need to define each column with an expression and set the related width where it is allowed to use a property name (string) instead of the expression (see: GitHub issue #14676)

    …  |Format-Table @{e='SamAccountName';width=30}, @{e='Enabled';width=20}
    

    See calculated properties/format-list

    With regards to your comment:

    Thanks. But for this command, I would have to customize the width of each column, then try out many different widths to try and get the same amount of spacing/distance between each columns. Is there an easier way to get equal spacing between the columns

    I am afraid that the Format-Table cmdlet isn't as sophisticated as WinForms or Windows Presentation Foundation and doesn't have something like a -stretch parameter. Besides it is quiet hard to determine how the data in the column is divided especially in a active pipeline and where the data could even exceed the available column width as in the DistinguishedName column of your own example.

    Anyways, I see two possible workarounds:

    Take advantage of a quirk

    if the first column is a calculated one that specifies a custom width, the remaining line width is evenly distributed among those remaining columns that do not themselves specify a column width which is also described in @mklement0's helpful answer for Controlling column widths with Format-Table:

    … |Format-Table @{e='SamAccountName'; w=15},Enabled,whenChanged,DistinguishedName -Wrap |
        out-String -Width 90
    
    SamAccountName                   Enabled whenChanged              DistinguishedName
    --------------                   ------- -----------              -----------------
    iRon7                              False 5/10/2023 9:05:23 AM     CN=iRon7,OU=Test,OU=User
                                                                      s,DC=Consoto,DC=com
    

    Disadvantage: it will not change the spacing between a Right - and Left aligned columns (as between Enabled and whenChanged).

    Use a spacer

    Create an empty column with a "white" header name:

    $Spacer = @{n=' '; e=' '}
    
    … |Format-Table SamAccountName,$Spacer,Enabled,$Spacer,whenChanged,$Spacer,DistinguishedName
    
    SamAccountName   Enabled   whenChanged            DistinguishedName
    -------------- - ------- - -----------          - -----------------
    iRon7              False   5/10/2023 9:05:23 AM   CN=iRon7,OU=Test,OU=Users,DC=Consoto,DC=com
    

    Disadvantage: this places extra dashes (-) on the ruler.