Out-String

将输入对象作为字符串输出。

语法

Out-String
   [-Width <Int32>]
   [-NoNewline]
   [-InputObject <PSObject>]
   [<CommonParameters>]
Out-String
   [-Stream]
   [-Width <Int32>]
   [-InputObject <PSObject>]
   [<CommonParameters>]

说明

Out-String cmdlet 将输入对象转换为字符串。 默认情况下,Out-String 会累积字符串并将其作为单个字符串返回,但你可以使用 Stream 参数来指示 Out-String 一次返回一行或创建字符串数组。 此 cmdlet 用于在对象操作不太方便时像在传统 shell 中一样搜索和操作字符串输出。

PowerShell 还添加了可调用 Out-String -StreamOSS 函数,作为在管道中使用 Out-String 的捷径。

示例

示例 1:获取当前区域性并将数据转换为字符串

此示例获取当前用户的区域设置并将对象数据转换为字符串。

$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 得到的结果。 Property 参数使用星号 (*) 通配符来指定对象中包含的所有属性。

Out-String 使用 InputObject 参数指定存储在 $C 变量中的 CultureInfo 对象。 $C 中的对象将转换为字符串。

注意

若要查看 Out-String 数组,请将输出存储到变量,并使用数组索引来查看元素。 有关数组索引的详细信息,请参阅 about_Arrays

$str = Out-String -InputObject $C -Width 100

示例 2:使用对象

此示例演示了使用对象和使用字符串之间的差异。 该命令显示一个包含文本 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-StringOut-String 返回的单个字符串中查找文本 gcm。

示例 3:使用 Width 参数防止截断。

虽然 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

参数

-InputObject

指定要写入字符串的对象。 输入一个包含对象的变量,或键入可获取对象的命令或表达式。

Type:PSObject
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-NoNewline

从 PowerShell 格式化程序生成的输出中移除所有换行符。 作为字符串对象一部分的换行符将被保留。

此参数是在 PowerShell 6.0 中引入的。

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Stream

默认情况下,Out-String 输出单个字符串,该字符串的格式与你在控制台中看到的格式相同,包括任何空白头部或尾部换行符。 Stream 参数使 Out-String 能够逐行输出每一行。 唯一的例外是多行字符串。 在这种情况下,Out-String 仍会将字符串输出为单个多行字符串。

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Width

指定输出的每一行中的字符数。 任何其他字符将被换行或截断,具体取决于使用的格式化程序 cmdlet。 Width 参数仅适用于正在格式化的对象。 如果省略此参数,则由主机程序的特征确定宽度。 在终端(控制台)窗口中,当前窗口宽度用作默认值。 PowerShell 控制台在安装后,其默认窗口宽度为 80 个字符。

Type:Int32
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

输入

PSObject

你可以通过管道将任何对象传递给此 cmdlet。

输出

String

此 cmdlet 返回它基于输入对象创建的字符串。

备注

包含 Out 谓词的 cmdlet 不会格式化对象。 Out cmdlet 将对象发送到指定的显示目标的格式化程序。

PowerShell 7.2 增加了用于控制 ANSI 转义序列呈现方式的功能。 可以根据 $PSStyle.OutputRendering 属性的设置更改传递给 Out-String 的 ANSI 修饰输出。 有关详细信息,请参阅 about_ANSI_Terminals