默认排序属性

时间:2015-01-09 12:42:58

标签: powershell object-properties

使用PowerShell,我试图理解默认排序属性的概念。

根据此示例,为Sort-Object命令提供:

PS C:\>get-childitem | sort-object

由于未指定任何属性,Sort-Objectfiledirectory类型的对象使用默认排序属性,即Name

对于任何给定的类型,有没有办法知道它的默认排序属性?

3 个答案:

答案 0 :(得分:1)

根据我的理解,默认属性取自预定义类型的.ps1XML文件。但我在about_Format.PS1XML

中没有发现任何相关信息

答案 1 :(得分:0)

对于这个问题,我相信默认项目在路径中定义:C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ FileSystem.format.ps1xml

答案 2 :(得分:0)

默认排序属性在 types.ps1xml 文件(在 $PSHOME 目录中)中定义。来自 Sort-Object 的在线帮助:

<块引用>

Sort-Object cmdlet 根据命令中指定的属性或对象类型的默认排序属性对对象进行排序。默认排序属性是使用 types.ps1xml 文件中名为 DefaultKeyPropertySet 的 PropertySet 定义的。有关详细信息,请参阅 about_Types.ps1xml

您可以使用 Update-TypeData 更新当前会话的 DefaultKeyPropertySet 值。我在下面提供了几个示例。

注意:在这些示例中,Sort-ObjectSelect-Object 之前应用,因此排序应用于从 Get-ChildItem 出来的对象,而不是从 Select-Object 出来的对象。

使用命令参数更新

这种技术只允许每个会话设置一次值。

# Inspect the current sort properties: not set
PS C:\Test>Get-TypeData -TypeName System.IO.FileInfo | Select-Object -ExpandProperty DefaultKeyPropertySet

# See Get-ChildItem results before a change is made: sorting on name
PS C:\Test>Get-ChildItem -File | Sort-Object | Select-Object FullName,CreationTime

FullName      CreationTime
--------      ------------
C:\Test\A.txt 18-Apr-2021 17:09:22
C:\Test\B.txt 10-Apr-2021 19:44:59
C:\Test\C.txt 31-Mar-2021 12:53:01

# Update the sort properties
PS C:\Test>Update-TypeData -TypeName 'System.IO.FileInfo' -DefaultKeyPropertySet 'CreationTime','FullName'
PS C:\Test>Get-TypeData -TypeName System.IO.FileInfo | Select-Object -ExpandProperty DefaultKeyPropertySet

ReferencedProperties     IsHidden
--------------------     --------
{CreationTime, FullName}    False

# See the results after a change is made: sorting on creation time
PS C:\Test>Get-ChildItem -File | Sort-Object | Select-Object FullName,CreationTime

FullName      CreationTime
--------      ------------
C:\Test\C.txt 31-Mar-2021 12:53:01
C:\Test\B.txt 10-Apr-2021 19:44:59
C:\Test\A.txt 18-Apr-2021 17:09:22

# Update the sort properties again: error
PS C:\Test>Update-TypeData -TypeName 'System.IO.FileInfo' -DefaultKeyPropertySet 'FullName','CreationTime'
Update-TypeData : Error in TypeData "System.IO.FileInfo": The member DefaultKeyPropertySet is already present.
At line:1 char:1
+ Update-TypeData -TypeName 'System.IO.FileInfo' -DefaultKeyPropertySet ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Update-TypeData], RuntimeException
    + FullyQualifiedErrorId : TypesDynamicUpdateException,Microsoft.PowerShell.Commands.UpdateTypeDataCommand

使用自定义 xml 文件更新

此技术允许在每个会话中多次设置值。 使用 xml 文件 types_test.ps1xml,包含以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<Types>
  <Type>
    <Name>System.IO.FileInfo</Name>
    <Members>
      <MemberSet>
        <Name>PSStandardMembers</Name>
        <Members>
          <PropertySet>
            <Name>DefaultKeyPropertySet</Name>
            <ReferencedProperties>
              <Name>CreationTime</Name>
              <Name>FullName</Name>
            </ReferencedProperties>
          </PropertySet>
        </Members>
      </MemberSet>
    </Members>
  </Type>
</Types>
# Inspect the current sort properties: not set
PS C:\Test>Get-TypeData -TypeName System.IO.FileInfo | Select-Object -ExpandProperty DefaultKeyPropertySet

# See Get-ChildItem results before a change is made: sorting on name
PS C:\Test>Get-ChildItem -File | Sort-Object | Select-Object FullName,CreationTime

FullName      CreationTime
--------      ------------
C:\Test\A.txt 18-Apr-2021 17:09:22
C:\Test\B.txt 10-Apr-2021 19:44:59
C:\Test\C.txt 31-Mar-2021 12:53:01

# Update the sort properties
PS C:\Test>Update-TypeData -PrependPath 'D:\types_test.ps1xml'
PS C:\Test>Get-TypeData -TypeName System.IO.FileInfo | Select-Object -ExpandProperty DefaultKeyPropertySet

ReferencedProperties     IsHidden
--------------------     --------
{CreationTime, FullName}    False

# See the results after a change is made: sorting on creation time
PS C:\Test>Get-ChildItem -File | Sort-Object | Select-Object FullName,CreationTime

FullName      CreationTime
--------      ------------
C:\Test\C.txt 31-Mar-2021 12:53:01
C:\Test\B.txt 10-Apr-2021 19:44:59
C:\Test\A.txt 18-Apr-2021 17:09:22

# In types_test.ps1xml, switch the order of the elements under the <ReferencedProperties> tag

# Update the sort properties again: 
PS C:\Test>Update-TypeData -PrependPath 'D:\types_test.ps1xml'
PS C:\Test>Get-TypeData -TypeName System.IO.FileInfo | Select-Object -ExpandProperty DefaultKeyPropertySet

ReferencedProperties     IsHidden
--------------------     --------
{FullName, CreationTime}    False