默认排序 属性
Default sort property
使用 PowerShell,我试图理解 默认排序 属性.
的概念
根据这个例子,为Sort-Object
设置命令:
PS C:\>get-childitem | sort-object
因为没有指定属性,Sort-Object
对 file
和 directory
类型的对象使用 默认排序 属性 , 即 Name
.
有没有办法知道,对于任何给定的类型,它的默认排序是什么 属性?
根据我的理解,默认 属性 是从预定义类型的 .ps1XML
文件中获取的。但我在 about_Format.PS1XML
中什么也没找到
对于这个问题,我相信默认项是在路径中定义的:C:\Windows\System32\WindowsPowerShell\v1.0\FileSystem.format.ps1xml
默认排序 属性 在 types.ps1xml 文件中定义(在 $PSHOME
目录中)。来自 Sort-Object
的联机帮助:
The Sort-Object cmdlet sorts objects based on properties specified in the command or the default sort properties for the object type. Default sort properties are defined using the PropertySet named DefaultKeyPropertySet in a types.ps1xml file. For more information, see about_Types.ps1xml.
您可以使用 Update-TypeData
更新当前会话的 DefaultKeyPropertySet 值。我在下面提供了几个示例。
注意:在这些示例中,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
使用 PowerShell,我试图理解 默认排序 属性.
的概念根据这个例子,为Sort-Object
设置命令:
PS C:\>get-childitem | sort-object
因为没有指定属性,Sort-Object
对 file
和 directory
类型的对象使用 默认排序 属性 , 即 Name
.
有没有办法知道,对于任何给定的类型,它的默认排序是什么 属性?
根据我的理解,默认 属性 是从预定义类型的 .ps1XML
文件中获取的。但我在 about_Format.PS1XML
对于这个问题,我相信默认项是在路径中定义的:C:\Windows\System32\WindowsPowerShell\v1.0\FileSystem.format.ps1xml
默认排序 属性 在 types.ps1xml 文件中定义(在 $PSHOME
目录中)。来自 Sort-Object
的联机帮助:
The Sort-Object cmdlet sorts objects based on properties specified in the command or the default sort properties for the object type. Default sort properties are defined using the PropertySet named DefaultKeyPropertySet in a types.ps1xml file. For more information, see about_Types.ps1xml.
您可以使用 Update-TypeData
更新当前会话的 DefaultKeyPropertySet 值。我在下面提供了几个示例。
注意:在这些示例中,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