about_Format.ps1xml

適用於: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

主題

about_Format.ps1xml

簡短描述

在 Windows PowerShell® 中的 Format.ps1xml 檔案能定義 Windows PowerShell 主控台中的物件預設顯示。您可以建立自己的 Format.ps1xml 檔案來變更物件顯示,或定義您在 Windows PowerShell 中建立之新物件類型的預設顯示。

詳細描述

Windows PowerShell 中的 Format.ps1xml 檔案能定義 Windows PowerShell 中物件的預設顯示。您可以建立自己的 Format.ps1xml 檔案來變更物件顯示,或定義您在 Windows PowerShell 中建立之新物件類型的預設顯示。

當 Windows PowerShell 顯示物件時,其會使用結構化調整格式檔案中的資料來判斷物件的預設顯示。調整格式檔案中的資料會判斷此物件要呈現在資料表或清單中,並判斷依預設會顯示哪些屬性。

調整格式只會影響顯示方式。其不會影響哪些物件屬性會傳遞到管線,也不會影響傳遞方式。Format.ps1xml 檔案無法用來自訂雜湊表的輸出格式。

Windows PowerShell 包含七個調整格式檔案。這些檔案位於安裝目錄中 ($pshome)。每個檔案會定義一組 Microsoft .NET Framework 物件的顯示方式:

Certificate.Format.ps1xml

憑證存放區中的物件,例如 X.509 憑證和憑證存放區。

DotNetTypes.Format.ps1xml

其他 .NET Framework 類型,例如 CultureInfo、FileVersionInfo 和 EventLogEntry 物件。

FileSystem.Format.ps1xml

檔案系統物件,例如檔案和目錄。

Help.Format.ps1xml

說明檢視,例如詳細且完整的檢視、參數和範例。

PowerShellCore.format.ps1xml

由 Windows PowerShell 核心 Cmdlet 所產生的物件,例如 Get-Member 和 Get-History。

PowerShellTrace.format.ps1xml

追蹤物件,例如 Trace-Command Cmdlet 所產生的物件。

Registry.format.ps1xml

登錄物件,例如機碼和項目。

調整格式檔案可以定義每個物件四個不同的檢視外觀:表格、清單、橫式和自訂。例如,當 Get-ChildItem 命令的輸出經由管道輸出至 Format-List 命令時,Format-List 會使用 FileSystem.format.ps1xml 檔案中的檢視來決定如何將檔案和資料夾物件顯示為清單。

當調整格式檔案包含多個物件的檢視時,Windows PowerShell 會套用其找到的第一個檢視。

在 Format.ps1xml 檔案中,檢視是由一組 XML 標籤來定義,而該標籤會描述檢視的名稱、可套用的物件型別、欄標題,和檢視主體中顯示的屬性。在 Format.ps1xml 檔案中的格式會在資料呈現給使用者之前套用。

建立新的 Format.ps1xml 檔案

透過 Windows PowerShell 安裝的 .ps1xml 檔案已經過數位簽章,防止因為調整格式可以包含指令碼區塊而遭他人篡改。因此,若要變更現有物件的檢視顯示格式或加入新物件的檢視,請建立您自己的 Format.ps1xml 檔案,然後將其加入您的 Windows PowerShell 工作階段中。

若要建立新的檔案,請複製現有的 Format.ps1xml 檔案。新的檔案可以取任何名稱,但必須具有 .ps1xml 副檔名。您可以將新的檔案放在 Windows PowerShell 可以存取的任何目錄,但是將檔案放置在 Windows PowerShell 安裝目錄 ($pshome) 或安裝目錄的子目錄中會更加實用。

若要變更目前檢視的格式,請在調整格式檔案中尋找檢視,然後使用標記來變更檢視。若要建立新物件類型的檢視,請建立新的檢視,或使用現有的檢視做為模型。(本主題的下一節會描述標記)。接著您就可以刪除該檔案中所有的其他檢視,這樣任何檢查檔案的人便能更明顯地看到變更。

儲存變更後,請使用 Update-FormatData Cmdlet 將新的檔案加入您的 Windows PowerShell 工作階段中。若要使您的檢視優先順序高於內建檔案中所定義的檢視,請使用 Update-FormatData 中的 PrependData 參數。Update-FormatData 只影響目前的工作階段。若要對後續的所有工作階段進行變更,請將 Update-FormatData 命令加入您的 Windows PowerShell 設定檔中。

範例:將行事曆資料加入文化特性物件中

這個範例將示範如何變更由 Get-Culture Cmdlet 所產生之文化特性物件 (System.Globalization.CultureInfo) 的格式。此範例中的命令會將行事曆屬性加入文化特性物件的預設資料表檢視顯示中。

第一步是尋找 Format.ps1xml 檔案,該檔案包含文化特性物件目前的檢視。下列 Select-String 命令會找到檔案:

  select-string -path $pshome\*format.ps1xml `
              -pattern System.Globalization.CultureInfo

此命令會顯示該定義位於 DotNetTypes.Format.ps1xml 檔案中。

下一個命令會將檔案內容複製到新的檔案,MyDotNetTypes.Format.ps1xml。

  copy-item DotNetTypes.Format.ps1xml MyDotNetTypes.Format.ps1xml

接下來,在任何 XML 或文字編輯器 (如 [記事本]) 中開啟 MyDotNetTypes.Format.ps1xml 檔案。尋找 System.Globalization.CultureInfo 物件一節。下列 XML 程式碼會定義 CultureInfo 物件的檢視。該物件只有 TableControl 檢視。

      <View>
          <Name>System.Globalization.CultureInfo</Name>
          <ViewSelectedBy>
             <TypeName>Deserialized.System.Globalization.CultureInfo</TypeName>
             <TypeName>System.Globalization.CultureInfo</TypeName>
          </ViewSelectedBy>

          <TableControl>
              <TableHeaders>
                  <TableColumnHeader>
                      <Width>16</Width>
                  </TableColumnHeader>
                  <TableColumnHeader>
                      <Width>16</Width>
                  </TableColumnHeader>
                  <TableColumnHeader/>
              </TableHeaders>
              <TableRowEntries>
                  <TableRowEntry>
                      <TableColumnItems>
                          <TableColumnItem>
                              <PropertyName>LCID</PropertyName>
                          </TableColumnItem>
                          <TableColumnItem>
                              <PropertyName>Name</PropertyName>
                          </TableColumnItem>
                          <TableColumnItem>
                              <PropertyName>DisplayName</PropertyName>
                          </TableColumnItem>
                      </TableColumnItems>
                  </TableRowEntry>
               </TableRowEntries>
          </TableControl>
      </View>

刪除檔案的其餘部分,但保留開頭的 <?XML>、<Configuration> 和 <ViewDefinitions> 標記,以及結束的 <ViewDefintions> 和 <Configuration> 標記。同時,您必須在每次變更檔案後刪除數位簽章。

      <?xml version="1.0" encoding="utf-8" ?>
      <Configuration>
          <ViewDefinitions>
              <View>
                  <Name>System.Globalization.CultureInfo</Name>
                  <ViewSelectedBy>
                      <TypeName>Deserialized.System.Globalization.CultureInfo</TypeName>
                      <TypeName>System.Globalization.CultureInfo</TypeName>
                  </ViewSelectedBy>

                  <TableControl>
                      <TableHeaders>
                          <TableColumnHeader>
                              <Width>16</Width>
                          </TableColumnHeader>
                          <TableColumnHeader>
                              <Width>16</Width>
                          </TableColumnHeader>
                          <TableColumnHeader/>
                      </TableHeaders>
                      <TableRowEntries>
                          <TableRowEntry>
                              <TableColumnItems>
                                  <TableColumnItem>
                                      <PropertyName>LCID</PropertyName>
                                  </TableColumnItem>
                                  <TableColumnItem>
                                      <PropertyName>Name</PropertyName>
                                  </TableColumnItem>
                                  <TableColumnItem>
                                      <PropertyName>DisplayName</PropertyName>
                                  </TableColumnItem>
                              </TableColumnItems>
                          </TableRowEntry>
                       </TableRowEntries>
                  </TableControl>
              </View>
          </ViewDefinitions>
      </Configuration>

接下來,加入一組新的 <TableColumnHeader> 標記來建立行事曆屬性的新資料行。行事曆屬性的值可能很長,因此這裡使用 45 個字元的值,如下所示:

                <TableControl>
                    <TableHeaders>
                        <TableColumnHeader>
                            <Width>16</Width>
                        </TableColumnHeader>
                        <TableColumnHeader>
                            <Width>16</Width>
                        </TableColumnHeader>
                 
                        <TableColumnHeader>
                            <Width>45</Width>
                        </TableColumnHeader>

                        <TableColumnHeader/>
                    </TableHeaders>

現在,將新的資料行項目加入資料表列中,如下所示:

                <TableRowEntries>
                    <TableRowEntry>
                        <TableColumnItems>
                            <TableColumnItem>
                                <PropertyName>LCID</PropertyName>
                            </TableColumnItem>
                            <TableColumnItem>
                                <PropertyName>Name</PropertyName>
                            </TableColumnItem>

                            <TableColumnItem>
                                <PropertyName>Calendar</PropertyName>
                            </TableColumnItem>

                            <TableColumnItem>
                                <PropertyName>DisplayName</PropertyName>
                            </TableColumnItem>
                        </TableColumnItems>
                    </TableRowEntry>
                 </TableRowEntries>

在儲存檔案並關閉後,使用 Update-FormatData 命令 (例如下列命令) 將新的格式檔案加入目前的工作階段中。此命令會使用 PrependData 參數,使新檔案的優先順序高於原始檔案。(如需 Update-FormatData 的詳細資訊,請輸入 "get-help update-formatdata"。)

  update-formatdata -prependpath $pshome\MyDotNetTypes.format.ps1xml

若要測試變更,請輸入 "get-culture",然後再檢閱輸出,該輸出包括行事曆屬性。

      C:\PS> get-culture

      LCID Name  Calendar                               DisplayName
      ---- ----  --------                               -----------
      1033 en-US System.Globalization.GregorianCalendar English (United States)

Format.ps1xml 檔案中的 XML

每個 Format.ps1xml 檔案的 ViewDefinitions 區段皆包含 <View> 標記,該標記會定義每個檢視。典型的 <View> 標記會包含下列標記:

<名稱>

<Name> 標記會識別檢視的名稱。

<ViewSelectedBy>

<ViewSelectedBy> 標記會指定物件類型或已為檢視所套用的類型。

<GroupBy>

<GroupBy> 標記會指定檢視中之項目組成群組的方式。

<TableControl>

<ListControl>

<WideControl>

<CustomControl>

這些標記包含 <DefaultDisplayProperty> 標記,該標記會指定每個項目的顯示方式。

<ViewSelectedBy> 標記可包含 <TypeName> 標記,其適用於每個檢視所套用的物件類型。或者也可以包含 <SelectionSetName> 標記,其會參考在其他地方使用 <SelectionSet> 標記所定義的選取集。

<GroupBy> 標記包含 <PropertyName> 標記,該標記會指定項目分組所依據的物件屬性。其也會包含 <Label> 標記 (該標記會為每個群組指定做為標籤的字串),或是 <CustomControlName> 標記 (該標記會參考在其他地方使用 <Control> 標記來定義的自訂控制項)。<Control> 標記包含 <Name> 標記和 <CustomControl> 標記。

<TableControl> 標記通常包含 <TableHeaders> 和 <TableRowEntries> 標記 (該標記會定義資料表的標頭和資料列格式)。<TableHeaders> 標記通常包含 <TableColumnHeader> 標記,而後者包含 <Label>、<Width>,和 <Alignment> 標記。<TableRowEntries> 標記包含資料表中每個資料列的 <TableRowEntry> 標記。<TableRowEntry> 標記包含 <TableColumnItems> 標記,而後者包含資料列中每個資料行的 <TableColumnItem> 標記。一般而言,<TableColumnItem> 標記包含 <PropertyName> 標記 (其可識別要在定義的位置中顯示的物件屬性),或 <ScriptBlock> 標記 (其包含的指令碼能計算要在位置中顯示的結果)。

注意:指令碼區塊也可以用在計算結果可派上用場的其他地方。

<TableColumnItem> 標記也可以包含 <FormatString> 標記,而後者會指定屬性或計算結果的顯示方式。

<ListControl> 標記通常包含 <ListEntries> 標記。<ListEntries> 標記包含 <ListItems> 標記。<ListItems> 標記包含 <ListItem> 標記,而後者包含 <PropertyName> 標記。<PropertyName> 標記會指定要顯示在清單中指定位置的物件屬性。如果使用選取集合來定義檢視選取項目的範圍,則 <ListControl> 標記也可包含 <EntrySelectedBy> ,其包含一或多個 <TypeName> 標記。這些 <TypeName> 標記會指定 <ListControl> 標記預定要顯示的物件類型。

<WideControl> 標記通常包含 <WideEntries> 標記。<WideEntries> 標記包含一或多個 <WideEntry> 標記。<WideEntry> 標記通常包含 <PropertyName> 標記,而後者會指定要在檢視中顯示於指定位置的屬性。<PropertyName> 標記可包含 <FormatString> 標記,而後者會指定屬性的顯示方法。

<CustomControl> 標記讓您可以使用指令碼區塊來定義格式。<CustomControl> 標記通常包含<CustomEntries> 標記,而後者包含多個 <CustomEntry> 標記。每個 <CustomEntry> 標記包含 <CustomItem> 標記,而後者可以包含各種不同的標記來指定檢視中指定位置的內容與格式,包括 <Text>、<Indentation>、<ExpressionBinding>,和 <NewLine> 標記。

Update-FormatData

若要將您 Format.ps1xml 檔案載入 Windows PowerShell 工作階段中,請使用 Update-FormatData Cmdlet。若希望您檔案中的檢視之優先順序高於內建 Format.ps1xml 檔案中的檢視,請使用 Update-FormatData 的 PrependData 參數。Update-FormatData 只影響目前的工作階段。若要對後續的所有工作階段進行變更,請將 Update-FormatData 命令加入您的 Windows PowerShell 設定檔中。

Types.ps1xml 中的預設顯示

一些基本物件類型的預設顯示會由 $pshome 目錄中的 Types.ps1xml 檔案來定義。節點的名稱是 PsStandardMembers,而子節點會使用下列其中一個標記:

<DefaultDisplayProperty>

<DefaultDisplayPropertySet>

<DefaultKeyPropertySet>

如需詳細資訊,請輸入下列命令:

  get-help about_types.ps1xml

追蹤 Format.ps1xml 檔案的使用情況

若要在 Format.ps1xml 檔案的載入或應用程式中偵測錯誤,請使用 Trace-Command Cmdlet,並使用下列任何一個格式元件做為 Name 參數的值:

  FormatFileLoading
          UpdateFormatData
          FormatViewBinding

如需詳細資訊,請輸入下列命令:

          get-help trace-command
          get-help get-tracesource     

簽署 Format.ps1xml 檔案

若要保護 Format.ps1xml 檔案的使用者,請使用數位簽章來簽署檔案。如需詳細資訊,請輸入:

          get-help about_signing

另請參閱

Update-FormatData

Trace-Command

Get-TraceSource