Out-String
輸出輸入物件為字串。
語法
NoNewLineFormatting (預設值)
Out-String
[-Width <Int32>]
[-NoNewline]
[-InputObject <PSObject>]
[<CommonParameters>]
StreamFormatting
Out-String
[-Stream]
[-Width <Int32>]
[-InputObject <PSObject>]
[<CommonParameters>]
Description
Out-String指令長 Ctrl 將輸入物件轉換成字串。 預設情況下,會 Out-String 累積字串並以單一字串回傳,但你可以使用 Stream 參數來導 Out-String 向一次回傳一行,或建立字串陣列。 這個 cmdlet 讓你能像傳統 shell 一樣搜尋和操作字串輸出,因為物件操作較不方便。
PowerShell 也新增 oss 了 call Out-String -Stream 這個函式,作為在管線中的簡寫方式 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 物件會被轉換成字串。
Note
要查看 Out-String 陣列,將輸出儲存為變數,並使用陣列索引來查看元素。 欲了解更多陣列索引資訊,請參見 about_Arrays。
$str = Out-String -InputObject $C -Width 100
範例二:與物件工作
這個例子展示了處理物件與處理字串之間的差異。 指令顯示包含文字 gcm(別名 for .)的別名 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 的匹配。
Note
如果你省略 Stream 參數,指令會顯示所有別名,因為 Select-String 會在回傳的單一字串Out-String中找到文字 gcm。
範例 3:使用寬度參數防止截斷
雖然大多數輸出 Out-String 會被換行到下一行,但有些情況是格式系統會截斷輸出再傳給 Out-String。 你可以用 寬度 參數避免截斷。
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
指定要寫入字串的物件。 輸入包含 物件的變數,或輸入取得物件的命令或表達式。
參數屬性
| 類型: | PSObject |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-NoNewline
移除 PowerShell 格式化器產生的所有換行。 屬於字串物件的換行會被保留。
此參數是在 PowerShell 6.0 中引進的。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
NoNewLineFormatting
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Stream
預設情況下, Out-String 輸出一個字串,格式與你在主控台中看到的相同,包含任何空白標頭或後續換行。
串流參數允許Out-String輸出每行。 唯一的例外是多行字串。 在這種情況下, Out-String 仍會輸出字串為單行多行字串。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
StreamFormatting
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Width
指定每行輸出的字元數。 任何額外的字元會根據格式化指令包(formatter cmdlet)被換行或截斷。 寬度參數僅適用於正在格式化的物件。 若省略此參數,寬度將由主機程式的特性決定。 在終端機(主控台)視窗中,預設值是目前視窗寬度。 PowerShell 主控台 Windows 安裝時預設寬度為 80 字元。
參數屬性
| 類型: | Int32 |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 cmdlet 支援常見參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
PSObject
您可以將任何物件通過管道傳遞至此命令工具。
輸出
String
這個 cmdlet 會回傳它從輸入物件建立的字串。
備註
包含動詞的 Out cmdlet 不會格式化物件。 cmdlets 將 Out 物件傳送到指定顯示目的地的格式化器。
PowerShell 7.2 新增了控制 ANSI 轉義序列渲染的能力。 傳遞給 Out-String ANSI 裝飾的輸出可根據屬性設定 $PSStyle.OutputRendering 進行調整。 如需詳細資訊,請參閱 about_ANSI_Terminals。