检索默认属性名称

dha*_*ech 4 powershell

以下命令:

Get-ChildItem | Get-Member

System.IO.DirectoryInfo表明 和上当然有很多属性System.IO.FileInfo

但是,当以交互方式运行时,仅显示属性ModeLastWriteTimeLength和。这些也是通过管道传输到或Name时显示的属性。Get-ChildItemFormat-TableOut-GridView

我的问题是,有没有办法以编程方式获取任何给定对象的这些默认属性的名称?即像这样:

Get-DefaultProperties @(Get-ChildItem)[0]
Run Code Online (Sandbox Code Playgroud)

应在数组中返回以下内容:Mode LastWriteTime Length Name

Jes*_*ake 5

内置解决方案

下面是访问默认属性的简单编程方法。

(Get-Item -Path $FolderPath).PSStandardMembers   
Run Code Online (Sandbox Code Playgroud)

生成的输出将是DefaultDisplayPropertyDefaultDisplayPropertySet


DefaultDisplayProperty  
----------------------   
Name
Run Code Online (Sandbox Code Playgroud)

或者

PSStandardMembers {DefaultDisplayPropertySet}    
Run Code Online (Sandbox Code Playgroud)

在第二种情况下,您可以像这样访问底层属性:

(Get-Item -Path $FilePath).PSStandardMembers.DefaultDisplayPropertySet   
Run Code Online (Sandbox Code Playgroud)
ReferencedPropertyNames : {LastWriteTime, Length, Name}  
MemberType              : PropertySet   
Value                   : DefaultDisplayPropertySet {LastWriteTime, Length, Name}   
TypeNameOfValue         : System.Management.Automation.PSPropertySet   
Name                    : DefaultDisplayPropertySet   
IsInstance              : False   
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用点表示法直接进入ReferencedPropertyNames数组。