Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Select-Object and Alter Property Names when modifying values

Tags:

powershell

I have a Cmdlet Get-AdUser that I am piping to a select object and in that select running a function on the AccountExpires property to convert the Property Value to a date value.

My question is when the object is output the column heading is no longer friendly and inherits the function used to return the new value. How do I form the statement so a friendly name is output? Was hoping to not have to assemble my own PSObject.

Get-AdUser "some.account" -Properties AccountExpires | select Enabled, Name, $({[datetime]$d =$_.AccountExpires; ConvertTo-Date($d)})

I then get a resulting table header of...

Enabled       True       [datetime]$d =$_.AccountExpires; ConvertTo-Date($d)
-------       -----      ----------------------------------------------------
like image 551
ATek Avatar asked Mar 16 '26 12:03

ATek


1 Answers

Use an expression hashtable of @{E={scriptblock};L="Label"}.

select Enabled, Name, @{E={$({[datetime]$d =$_.AccountExpires; ConvertTo-Date($d)})};L="AccountExpires"}

See http://technet.microsoft.com/en-us/library/ee692794.aspx for more information on creating custom selected objects.

like image 200
HighlyUnavailable Avatar answered Mar 18 '26 04:03

HighlyUnavailable



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!