如何使用 PowerShell 输出带有前导制表符的字符串?
How to output strings with leading tab using PowerShell?
在 PowerShell 中,假设我有一个字符串对象集合:
'a b c'.split()
我可以将这些通过管道传输到输出流:
'a b c'.split() | Write-Host
所以我在不同的行上得到 "a"、"b" 和 "c"。
现在:我该如何更改它以便每一行(字符串)都有一个前导制表符?
我知道制表符由 `t 表示,所以我的第一次尝试是这样的:
'a b c'.split() | Write-Host "`t$_"
这给了我错误:
Write-Host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and
its properties do not match any of the parameters that take pipeline input.
好吧,让我们取出选项卡并使用 $_,我认为它代表管道返回的 'current' 对象。
'a b c'.split() | Write-Host $_
同样的错误:
Write-Host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and
its properties do not match any of the parameters that take pipeline input.
这里显然有一些我从根本上误解的地方,所以我希望有人能指出那是什么!另外,忽略 Write-Host
的使用:我只是为了说明目的使用它,我的目的是最终通过管道传输到 Write-Verbose
。
试试这个:
'a b c'.split() | foreach {write-host "`t$_"}
试试这个
'a b c'.split() | %{Write-Host "`t$_"}
或短
'a b c'.split() | %{"`t$_"}
或这个
'a b c'.Split() | select @{N="Value";E={"`t$_"}}
在 PowerShell 中,假设我有一个字符串对象集合:
'a b c'.split()
我可以将这些通过管道传输到输出流:
'a b c'.split() | Write-Host
所以我在不同的行上得到 "a"、"b" 和 "c"。
现在:我该如何更改它以便每一行(字符串)都有一个前导制表符?
我知道制表符由 `t 表示,所以我的第一次尝试是这样的:
'a b c'.split() | Write-Host "`t$_"
这给了我错误:
Write-Host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
好吧,让我们取出选项卡并使用 $_,我认为它代表管道返回的 'current' 对象。
'a b c'.split() | Write-Host $_
同样的错误:
Write-Host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
这里显然有一些我从根本上误解的地方,所以我希望有人能指出那是什么!另外,忽略 Write-Host
的使用:我只是为了说明目的使用它,我的目的是最终通过管道传输到 Write-Verbose
。
试试这个:
'a b c'.split() | foreach {write-host "`t$_"}
试试这个
'a b c'.split() | %{Write-Host "`t$_"}
或短
'a b c'.split() | %{"`t$_"}
或这个
'a b c'.Split() | select @{N="Value";E={"`t$_"}}