在PowerShell脚本中缩进格式表输出

在PowerShell脚本中缩进格式表输出,powershell,indentation,Powershell,Indentation,如何将cmdlet的输出缩进到特定列 我有: > $SomeValues | Format-Table -HideTableHeaders A 1 B 2 C 3 但我想: A 1 B 2 C 3 使用: 使用: 实际上,您可以使用ft和-autosize $a= @{A=1;B=2;C=3} $a.getenumerator() | ft blah,name,value -hidetableheaders -auto

如何将cmdlet的输出缩进到特定列

我有:

> $SomeValues | Format-Table -HideTableHeaders
A    1
B    2
C    3
但我想:

    A    1
    B    2
    C    3
使用:

使用:


实际上,您可以使用ft和-autosize

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

      A        1
      B        2
      C        3

谢谢大家的回答。他们帮助我找到了如何使用计算属性来实现我想要的功能。
表达式
必须比缩进量少一个,这是由于表中列之间的自动单字符空格造成的

如果您使用的是
-AutoSize
标志:

Write-Host "Not indented"
Write-Host "    Indented"
$a = @{ Aa = 1; Bbb = 2; Cccc = 300}
$a | Format-Table -Property @{Expression="   "},Name,Value -AutoSize -HideTableHeaders
Write-Host "Not indented"
Write-Host "    Indented"
$a = @{ Aa = 1; Bbb = 2; Cccc = 300}
$a | Format-Table -Property @{Expression={}; Width=3},Name,Value -HideTableHeaders
如果未使用
-AutoSize
标志:

Write-Host "Not indented"
Write-Host "    Indented"
$a = @{ Aa = 1; Bbb = 2; Cccc = 300}
$a | Format-Table -Property @{Expression="   "},Name,Value -AutoSize -HideTableHeaders
Write-Host "Not indented"
Write-Host "    Indented"
$a = @{ Aa = 1; Bbb = 2; Cccc = 300}
$a | Format-Table -Property @{Expression={}; Width=3},Name,Value -HideTableHeaders
输出如下所示:

Not indented
    Indented

    Bbb      2
    Aa       1
    Cccc   300

对于通用解决方案

 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) "
这应该可以

 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) "

还有一种方法不需要创建额外的列。您只需从Format Table命令获取正常输出,使用Out String将其转换为字符串数组,然后使用ForEach Object打印带有填充的每个字符串。下面是一个使用Get进程的示例


你是说自动调整尺寸?这与问题无关,该选项只会更改列宽。啊..我的错。我误解了。在这种情况下,您可以使用格式说明符,而不使用format TableClose,但我更希望这两列左对齐。我尝试了您的示例,第一列右对齐。$a.GetEnumerator()|%{{{{0,5}{1,5}”-f$\u.key,$\u.value}调用管道后的语法是什么?我想了解一下。这是.Net格式说明符语法。例如,@SalvatoreDiFazio:你是说在数字栏中?这是PowerShell打印数字的默认方式。这很有用,因为所有的小数位都排成一行,便于比较。在我的例子中,我有很多“-”字符,信息在我的powershell控制台的右侧,这更好
$indent = "    "
(Get-Process svchost) | Format-Table -Property Id, ProcessName | Out-String -Stream | ForEach-Object {Write-Output "$indent$_"}