2

How can I indent output from the Format-Table cmdlet to a specific column?

I have:

> $SomeValues | Format-Table -HideTableHeaders
A    1
B    2
C    3

But I'd like:

    A    1
    B    2
    C    3

7 Answers 7

10

Thanks everyone for your answers. They helped me figure out how to do what I wanted using calculated properties. The Expression must be one less than the amount of indent, due to the automatic single character space between columns in the table.

If you're using the -AutoSize flag:

Write-Host "Not indented"
Write-Host "    Indented"
$a = @{ Aa = 1; Bbb = 2; Cccc = 300}
$a | Format-Table -Property @{Expression="   "},Name,Value -AutoSize -HideTableHeaders

If you're not using the -AutoSize flag:

Write-Host "Not indented"
Write-Host "    Indented"
$a = @{ Aa = 1; Bbb = 2; Cccc = 300}
$a | Format-Table -Property @{Expression={}; Width=3},Name,Value -HideTableHeaders

The output looks like:

Not indented
    Indented

    Bbb      2
    Aa       1
    Cccc   300
Sign up to request clarification or add additional context in comments.

2 Comments

@SalvatoreDiFazio: You mean in the number's column? That's the default way PowerShell prints numbers. It's useful because then all the decimal places are lined up for easy comparison.
In my case I have a lot of '-' char and the information are at the right of my powershell console
3

There's another way that doesn't require creating an additional column. You can simply take the normal output from the Format-Table command, use Out-String to convert it to a string array, and then use ForEach-Object to print out each string with padding. Here's an example using Get-Process.

$indent = "    "
(Get-Process svchost) | Format-Table -Property Id, ProcessName | Out-String -Stream | ForEach-Object {Write-Output "$indent$_"}

1 Comment

This indents the header as well
2

Use:

PS> $a = @{A=1; B=2; C=3}
PS> $a.GetEnumerator() | %{ "{0,10}{1,5}" -f $_.key, $_.value }
         A    1
         B    2
         C    3

4 Comments

Close, but I'd prefer that both columns be left-aligned. I tried your example and the first column became right-aligned.
$a.GetEnumerator()| % { "{0,5}{1,5}" -f $_.key,$_.value}
What is the syntax after the pipe called? I'd like to read about it.
it is .Net format specifier syntax. One example, msdn.microsoft.com/en-us/library/1ksz8yb7(v=vs.90).aspx
0

Actually, you can use ft and -autosize

 $a= @{A=1;B=2;C=3}
 $a.getenumerator() | ft blah,name,value -hidetableheaders -auto

      A        1
      B        2
      C        3

Comments

0

This should do it

 function Indent-ConsoleOutput($output, $indent=4){
    if(!($output -eq $null)){
        if(!( $indent -is [string])){
            $indent = ''.PadRight($indent)
        }
        $width = (Get-Host).UI.RawUI.BufferSize.Width - $indent.length
        ($output| out-string).trim().replace( "`r", "").split("`n").trimend()| %{
            for($i=0; $i -le $_.length; $i+=$width){
                if(($i+$width) -le $_.length){ 
                    "$indent"+$_.substring($i, $width)
                }else{
                    "$indent"+$_.substring($i, $_.length - $i)
                }
            }
        }
    }
}

'##  Get-Process'
    Indent-ConsoleOutput ((get-process)[0..5]|format-table) 4
''
'## Log Stye Output'
    Indent-ConsoleOutput ((get-process)[0..5]|format-table) "    $(Get-Date) "

Comments

0

For a generic solution

 function Indent-ConsoleOutput($output, $indent=4){
    if(!($output -eq $null)){
        if(!( $indent -is [string])){
            $indent = ''.PadRight($indent)
        }
        $width = (Get-Host).UI.RawUI.BufferSize.Width - $indent.length
        ($output| out-string).trim().replace( "`r", "").split("`n").trimend()| %{
            for($i=0; $i -le $_.length; $i+=$width){
                if(($i+$width) -le $_.length){ 
                    "$indent"+$_.substring($i, $width)
                }else{
                    "$indent"+$_.substring($i, $_.length - $i)
                }
            }
        }
    }
}

'##  Get-Process'
Indent-ConsoleOutput ((get-process)[0..5]|format-table) 4
''
'## Log Stye Output'
Indent-ConsoleOutput ((get-process)[0..5]|format-table) "    $(Get-Date) "

1 Comment

This is much better
0

Use:

$SomeValues | Format-Table -HideTableHeaders -AutoSize

2 Comments

You mean -AutoSize? That has nothing to do with the question, that option just changes column widths.
Aah..My bad. I misread that. You can use format specifiers in that case and not Format-Table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.