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
My questions are
dir command has any additional headers? If yes how to get them?To complement @AbrahamZinala comment:
dir(alias toGet-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
Modeproperty 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With