Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell - get command's result header names

How to get in PowerShell command's result type? I'm interested how to achieve that from console command instead of searching documentation

For example: I type dir then command will return table with such headers

  • Mode
  • LastWriteTime
  • Length
  • Name

My questions are

  1. how to run command to get result headers for dir (these four string)?
  2. does dir command has any additional headers? If yes how to get them?
like image 852
Jacek Avatar asked Dec 06 '25 07:12

Jacek


1 Answers

To complement @AbrahamZinala comment:

dir (alias to Get-ChildItem) returns fileinfo, and directoryinfo objects. If you want to see all the members and methods that are available to you, you can pipe to Get-Member. If you'd like to select all properties than what is shown, you can pipe to Select-Object and specify individual properties, or all using *

By default Get-Member hides a few members along with the PSStandardMembers property:

Get-ChildItem -File | Get-Member -Name PSStandardMembers

These members will only reveal if you use the GetMember -Force switch:

Get-ChildItem -File | Get-Member -Name PSStandardMembers -Force

   TypeName: System.IO.FileInfo

Name              MemberType Definition
----              ---------- ----------
PSStandardMembers MemberSet  PSStandardMembers {DefaultDisplayPropertySet}

This property includes the members that are shown by default in the table format (aka '... | Format-Table')

(Get-ChildItem -File)[0].PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
LastWriteTime
Length
Name

I have no clue why the specific Mode property also shows up in table format while it isn't actually listed here and I would be please if someone can explain this part

The Select-Object will indeed remove this decoration:

Get-ChildItem -File | Select-Object * | Format-Table  -Property @{ e='*'; width = 7 }

PSPath  PSParen PSChild PSDrive PSProvi PSIsCon Mode    ModeWit Version BaseNam Resolve Target  LinkTyp  Length Directo Directo IsReadO FullNam Extensi Name     Exists Creatio Creatio LastAcc LastAcc LastWri LastWri LinkTar UnixFil Attribu
        tPath   Name            der      tainer         houtHar Info    e       dTarget         e               ryName  ry          nly e       on                      nTime   nTimeUt essTime essTime teTime  teTimeU get       eMode     tes
                                                        dLink                                                                                                                   c               Utc             tc
------  ------- ------- ------- ------- ------- ----    ------- ------- ------- ------- ------  -------  ------ ------- ------- ------- ------- ------- ----     ------ ------- ------- ------- ------- ------- ------- ------- ------- -------
Micros… Micros… dir.ps1 P       Micros…   False -a---   -a---   File: … dir     P:\Sta…                     383 P:\Sta… P:\Sta…   False P:\Sta… .ps1    dir.ps1    True 9/7/20… 9/7/20… 9/7/20… 9/7/20… 9/7/20… 9/7/20…              -1 Archive

But you might simply create your own DefaultDisplayPropertySet on each object like this:

$DefaultDisplayPropertySet = [System.Management.Automation.PSPropertySet]::new(
    'DefaultDisplayPropertySet',[string[]]('Name', 'FullName')
)
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]$DefaultDisplayPropertySet
$Dir = Get-ChildItem -File | Select-Object * | Foreach-Object {
    $_ | Add-Member MemberSet PSStandardMembers $PSStandardMembers -PassThru
}

The object list ($Dir) has still all standard properties available, e.g.:

$Dir.LastWriteTime

Thursday, September 7, 2023 11:07:32 AM

But it only shows just the two default properties when outputted to the display:

$Dir

Name    FullName
----    --------
dir.ps1 C:\FullPath\Test.ps1
like image 68
iRon Avatar answered Dec 08 '25 21:12

iRon