Out-String
輸出輸入物件做為字串。
語法
Out-String
[-Width <Int32>]
[-NoNewline]
[-InputObject <PSObject>]
[<CommonParameters>]
Out-String
[-Stream]
[-Width <Int32>]
[-InputObject <PSObject>]
[<CommonParameters>]
Description
Out-String
Cmdlet 會將輸入物件轉換成字串。 根據預設,Out-String
會累積字串並將它們當做單一字串傳回,但您可以使用 Stream 參數來指示 Out-String
一次傳回一行,或建立字元串陣列。 當物件作較不方便時,此 Cmdlet 可讓您搜尋及作字元串輸出,就像在傳統殼層中一樣。
PowerShell 也會新增 OSS
函式,此函式會呼叫 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
中的物件會轉換成字串。
注意
若要檢視 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-String
會在 Out-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
指定要寫入字串的物件。 輸入包含 物件的變數,或輸入取得物件的命令或表達式。
類型: | PSObject |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | True |
接受萬用字元: | False |
-NoNewline
從 PowerShell 格式器所產生的輸出中移除所有換行符。 保留屬於字串物件一部分的新行。
此參數是在 PowerShell 6.0 中引進的。
類型: | SwitchParameter |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Stream
根據預設,Out-String
輸出格式化的單一字串,如同您在控制台中看到,包括任何空白標頭或尾端換行符。
Stream 參數可讓 Out-String
逐行輸出。 唯一的例外是多行字串。 在此情況下,Out-String
仍會將字串輸出為單一多行字串。
類型: | SwitchParameter |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Width
指定每行輸出中的字元數。 任何其他字元都會包裝至下一行或截斷,視所使用的格式器 Cmdlet 而定。 Width 參數僅適用於格式化的物件。 如果您省略此參數,則寬度取決於主機程式的特性。 在終端機 (主控台) 視窗中,目前的視窗寬度會當做預設值使用。 PowerShell 控制台視窗預設為安裝時 80 個字元的寬度。
類型: | Int32 |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
輸入
您可以使用管線將任何物件傳送至此 Cmdlet。
輸出
這個 Cmdlet 會傳回它從輸入物件建立的字串。
備註
包含 Out
動詞命令的 Cmdlet 不會格式化物件。
Out
Cmdlet 會將物件傳送至指定顯示目的地的格式器。
PowerShell 7.2 新增了控制 ANSI 逸出序列轉譯方式的功能。 傳遞至 Out-String
的 ANSI 裝飾輸出可以根據 $PSStyle.OutputRendering
屬性的設定來改變。 如需詳細資訊,請參閱 about_ANSI_Terminals。