这让我有些意外,但显然,-Width 参数仅适用于格式化对象:
字符串作为输入,无效
PS C:\> "It is a nice hot sunny day" |Out-File '.\output.txt' -Width 10 -Force; gc '.\output.txt'
It is a nice hot sunny day
PS C:\> New-Object psobject -Property @{text="It is a nice hot sunny day"} | Format-Table |Out-File '.\output.txt' -Width 10 -Force; gc '.\output.txt'
text
-----
It is a...
PS C:\> New-Object psobject -Property @{text="It is a nice hot sunny day"} | Format-Table |Out-File '.\output.txt' -Width 10 -Force; gc '.\output.txt'
text : It
is
a n
ice
ho
t s
unn
y
day
所以,我们可以得到的最接近的可能是Format-Table -HideTableHeaders:
PS D:\> New-Object psobject -Property @{text="It is a nice hot sunny day"} | Format-Table -HideTableHeaders|Out-File '.\output.txt' -Width 10 -Force; gc '.\output.txt'
It is a...
受@Matt 回答的启发,您可以编写自己的函数来截断字符串:
Function Resize-String {
Param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string[]]$InputObject,
[int]$Width = 10
)
process{
$InputObject.Substring(0,[System.Math]::Min([string]$InputObject[0].Length,$Width))
}
}
PS C:\> "It is a nice hot sunny day" |Resize-String|Out-File '.\output.txt' -Width 10 -Force; gc '.\output.txt'
It is a ni