8

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

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

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

由于未指定任何属性,因此对and类型的对象Sort-Object使用默认排序属性,即.filedirectoryName

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

4

3 回答 3

1

据我了解,默认属性取自.ps1XML预定义类型的文件。但我在about_Format.PS1XML中一无所获

于 2015-01-09T13:19:12.337 回答
1

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

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

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

注意:在这些示例Sort-Object中是之前应用的,Select-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
于 2021-04-24T21:05:31.737 回答
0

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

于 2015-01-09T14:53:32.597 回答