Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select-Object - Keep default columns but add one

Tags:

powershell

I can use Select-Object to choose which columns to show and even add calculated columns. An example:

gci | select *, @{n='LAS'; e={(Get-Date)-$_.LastAccessTime}}

I want add a calculated column but keep the defaut ones. Without the * wildcard I only get my calculated property. With it I get everything. The only workaround I've got to work is to manually list the default property names. Any ideas?

like image 713
Steve Avatar asked Oct 15 '25 07:10

Steve


1 Answers

The thing is that that you actually states to display all properties ('*').

So to add to only the standard properties, you first need to get the standard properties.

[string[]]$StdProperties = (Get-ChildItem).PSStandardMembers.DefaultDisplayPropertySet[1].ReferencedPropertyNames

We don't actually want to change the standard property of the objects returned

Get-Childitem | select Name | Get-Member| group TypeName | select Name

Name
----
Selected.System.IO.DirectoryInfo
Selected.System.IO.FileInfo

So we just need to expand on that extracted string array with the new property to use.

$StdProperties += 'LAS'

And finally, to put it to use...

Get-ChildItem | select *, @{n='LAS'; e={(Get-Date) - $_.LastAccessTime}} | 
select $StdProperties
like image 171
Dennis Avatar answered Oct 18 '25 05:10

Dennis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!