Out-String
将输入对象输出为字符串。
Out-String
[-Stream]
[-Width <Int32>]
[-InputObject <PSObject>]
[<CommonParameters>]
Out-String
cmdlet 将输入对象转换为字符串。 默认情况下,Out-String
累积字符串并将其作为单个字符串返回,但可以使用 Stream 参数将 Out-String
一次返回一行或创建字符串数组。 此 cmdlet 允许在对象作不太方便时像在传统 shell 中一样搜索和作字符串输出。
PowerShell 还添加了 OSS
函数,该函数调用 Out-String -Stream
作为在管道中使用 Out-String
的简写方式。
此示例获取当前用户的区域设置,并将对象数据转换为字符串。
$C = Get-Culture | Select-Object -Property *
Out-String -InputObject $C -Width 100
Parent : en
LCID : 1033
KeyboardLayoutId : 1033
Name : en-US
IetfLanguageTag : en-US
DisplayName : English (United States)
NativeName : English (United States)
EnglishName : English (United States)
TwoLetterISOLanguageName : en
ThreeLetterISOLanguageName : eng
ThreeLetterWindowsLanguageName : ENU
CompareInfo : CompareInfo - en-US
TextInfo : TextInfo - en-US
IsNeutralCulture : False
CultureTypes : SpecificCultures, InstalledWin32Cultures, FrameworkCultures
NumberFormat : System.Globalization.NumberFormatInfo
DateTimeFormat : System.Globalization.DateTimeFormatInfo
Calendar : System.Globalization.GregorianCalendar
OptionalCalendars : {System.Globalization.GregorianCalendar,
System.Globalization.GregorianCalendar}
UseUserOverride : True
IsReadOnly : False
$C
变量存储 Selected.System.Globalization.CultureInfo 对象。 该对象是 Get-Culture
将输出发送到管道以 Select-Object
的结果。
属性 参数使用星号(*
)通配符来指定对象中包含的所有属性。
Out-String
使用 InputObject 参数来指定存储在 $C
变量中的 CultureInfo 对象。
$C
中的对象将转换为字符串。
备注
若要查看 Out-String
数组,请将输出存储到变量,并使用数组索引查看元素。 有关数组索引的详细信息,请参阅 about_Arrays。
$str = Out-String -InputObject $C -Width 100
此示例演示了使用对象和处理字符串之间的差异。 该命令显示包含文本 gcm(Get-Command
别名)的别名。
Get-Alias | Out-String -Stream | Select-String -Pattern "gcm"
Alias gcm -> Get-Command
Get-Alias
获取 System.Management.Automation.AliasInfo 对象,每个别名各有一个,并将对象发送到管道。
Out-String
使用 Stream 参数将每个对象转换为字符串,而不是将所有对象串联成单个字符串。
System.String 对象向下发送管道,Select-String
使用 Pattern 参数查找文本 gcm的匹配项。
备注
如果省略 Stream 参数,该命令将显示所有别名,因为 Select-String
查找 Out-String
返回的单个字符串中的文本 gcm。
虽然 Out-String
的大部分输出被包装到下一行,但在某些情况下,格式化系统先截断输出,然后再传递给 Out-String
。 可以使用 Width 参数避免截断。
PS> @{TestKey = ('x' * 200)} | Out-String
Name Value
---- -----
TestKey xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
PS> @{TestKey = ('x' * 200)} | Out-String -Width 250
Name Value
---- -----
TestKey xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
指定要写入字符串的对象。 输入包含对象的变量,或键入获取对象的命令或表达式。
类型: | PSObject |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | True |
接受通配符: | False |
默认情况下,Out-String
输出格式化的单个字符串,就像在控制台中看到的那样,包括任何空白标头或尾随换行符。
Stream 参数允许 Out-String
逐行输出。 唯一的例外是多行字符串。 在这种情况下,Out-String
仍将将字符串输出为单个多行字符串。
类型: | SwitchParameter |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
指定每行输出中的字符数。 任何其他字符都包装到下一行或截断,具体取决于所使用的格式化程序 cmdlet。 Width 参数仅适用于正在格式化的对象。 如果省略此参数,则宽度由主机程序的特征决定。 在终端(控制台)窗口中,当前窗口宽度用作默认值。 安装时,PowerShell 控制台窗口默认宽度为 80 个字符。
类型: | Int32 |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
可以通过管道将任何对象传递给此 cmdlet。
此 cmdlet 返回从输入对象创建的字符串。
包含 Out
谓词的 cmdlet 不会设置对象的格式。
Out
cmdlet 将对象发送到指定显示目标的格式化程序。