Get-FormatData
取得目前會話中的格式化數據。
語法
Default (預設值)
Get-FormatData
[[-TypeName] <String[]>]
[-PowerShellVersion <Version>]
[<CommonParameters>]
Description
Get-FormatData Cmdlet 會在目前的會話中取得格式化數據。
會話中的格式化數據包含來自 Format.ps1xml 格式化檔案的數據,例如位於 $PSHOME 目錄中的格式化數據,您匯入到會話中的模組格式化數據,以及您使用 Import-PSSession Cmdlet 匯入到您的會話中的命令格式化數據。
您可以使用此 Cmdlet 來檢查格式化數據。 然後,您可以使用 Export-FormatData Cmdlet 來串行化物件、將它們轉換成 XML,並將其儲存在 Format.ps1xml 檔案中。
如需在 PowerShell 中格式化檔案的詳細資訊,請參閱 about_Format.ps1xml。
範例
範例 1:取得所有格式化數據
這個範例會取得會話中的所有格式化數據。
Get-FormatData
範例 2:依類型名稱取得格式化數據
本範例會取得名稱開頭為 System.Management.Automation.Cmd的格式數據項。
Get-FormatData -TypeName 'System.Management.Automation.Cmd*'
範例 3:檢查格式化資料物件
此範例示範如何取得格式化數據物件,並檢查其屬性。
$F = Get-FormatData -TypeName 'System.Management.Automation.Cmd*'
$F
TypeName FormatViewDefinition
-------- --------------------
HelpInfoShort {help , TableControl}
$F.FormatViewDefinition[0].Control
Headers : {System.Management.Automation.TableControlColumnHeader,
System.Management.Automation.TableControlColumnHeader,
System.Management.Automation.TableControlColumnHeader,
System.Management.Automation.TableControlColumnHeader}
Rows : {System.Management.Automation.TableControlRow}
AutoSize : False
HideTableHeaders : False
GroupBy :
OutOfBand : False
$F.FormatViewDefinition[0].Control.Headers
Label Alignment Width
----- --------- -----
CommandType Undefined 15
Name Undefined 50
Version Undefined 10
Source Undefined 0
範例 4:取得格式化數據並將其匯出
此範例示範如何使用 Get-FormatData 和 Export-FormatData 匯出模組所新增的格式數據。
$A = Get-FormatData
Import-Module BitsTransfer
$B = Get-FormatData
Compare-Object $A $B
InputObject SideIndicator
----------- -------------
Microsoft.BackgroundIntelligentTransfer.Management.BitsJob =>
Get-FormatData *bits* | Export-FormatData -FilePath C:\test\Bits.format.ps1xml
Get-Content C:\test\Bits.format.ps1xml
<?xml version="1.0" encoding="utf-8"?><Configuration><ViewDefinitions>
<View><Name>Microsoft.BackgroundIntelligentTransfer.Management.BitsJob</Name>
...
前四個命令會使用 Get-FormatData、Import-Module和 Compare-Object Cmdlet 來識別 BitsTransfer 模組新增至會話的格式類型。
第五個命令會使用 Get-FormatData Cmdlet 來取得 BitsTransfer 模組新增的格式類型。 它會使用管線運算符 (|) 將格式類型物件傳送至 Export-FormatData Cmdlet,以將它轉換回 XML,並將它儲存在指定的 Bits.format.ps1xml 檔案中。
最後一個命令會顯示 format.ps1xml 檔案內容的摘錄。
範例 5:根據指定的 PowerShell 版本取得格式化數據
此範例示範如何使用 Get-FormatData 來取得指定之 TypeName 和 PowerShell 版本的格式數據。
Get-FormatData -TypeName 'Microsoft.Powershell.Utility.FileHash' -PowerShellVersion $PSVersionTable.PSVersion
TypeNames FormatViewDefinition
--------- --------------------
{Microsoft.Powershell.Utility.FileHash} {Microsoft.Powershell.Utility.FileHash}
參數
-PowerShellVersion
指定此 cmdlet 針對格式化數據取得的 PowerShell 版本。 輸入以句號分隔的兩位數數位。
此參數已在 PowerShell 5.1 中新增,以改善遠端至執行舊版 PowerShell 的電腦時的相容性。
參數屬性
| 類型: | Version |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-TypeName
指定此 Cmdlet 取得格式數據的類型名稱。 輸入類型名稱。 允許使用通配符。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
(All)
| Position: | 0 |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
None
您不能將物件透過管道傳送到此 Cmdlet。