Search code examples
powershellindentation

Indenting Format-Table output in PowerShell scripts


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

Solution

  • 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