0

I'm aware that we can specify a comma-separated string[] of properties in a variable to the Select-Object -Property parameter. However, I'm trying to include calculated properties in that variable. Tried adding it by enclosing them in single-quotes / double-quotes like,

  1. $selectProps = "distinguishedname", '@{n="lastlogontimestamp";e={[datetime]::FromFileTime($_.lastlogontimestamp).ToString()}}'

    and

  2. $selectProps = "distinguishedname","@{n=`"lastlogontimestamp`";e={[datetime]::FromFileTime(`$_.lastlogontimestamp).ToString()}}"

    but to no avail. Any help will be much appreciated.

Karthick Ganesan
  • 375
  • 4
  • 11

1 Answers1

2

If you put a string in a variable you should wrap that in quotes. But as the hashtable is not a string you don't have to put it in quotes. ;-)

$selectProps = @(
    'distinguishedname',
    @{ Name = 'lastlogontimestamp'; Expression = { [datetime]::FromFileTime($_.lastlogontimestamp).ToString() } }
)

It would work without wrapping the array in @() ... that's just a visual support to make it easier readable.

If you want to add more than one array with calculated properties to your Select-Object command you can add them together like this for example:

Select-Object -Property ($SelectProps + $SelectProps2)
Olaf
  • 4,690
  • 2
  • 15
  • 23
  • Hi Olaf, That makes sense :) ... I'd like to just rephrase my question. I'm wanting to add the calculated property hashtable to an existing `$selectProps` array of strings. How would I do that ? – Karthick Ganesan Apr 08 '20 at 08:06