0

I have a table variable that can be treated like a csv file...

"SamAccountName","lastLogonTimestamp","AccountExpires","givenname","sn","distinguishedName","employeeNumber","employeeID","Description","extensionattribute8","userAccountControl"
"value1","value1","value1","value1","value1","value1","value1","value1","value1","value1","value1"
"value2","value2","value2","value2","value2","value2","value2","value2","value2","value2","value2"
"value3","value3","value3","value3","value3","value3","value3","value3","value3","value3","value3"

What I want to do, is change the two title names givenname to FirstName, and sn to LastName.

Note: I also want to change the values for lastLogonTimestamp and AccountExpires, but I already have the working code that does this. This code is as follows...

$listOfBadDateValues = '9223372036854775807', '9223372036854770000', '0'
$maxDateValue = '12/31/1600 5:00 PM'

$tableFixed = $table | % { 
    if ($_.lastLogonTimestamp) {
        $_.lastLogonTimestamp = ([datetime]::FromFileTime($_.lastLogonTimestamp)).ToString('g')
    }; if (($_.AccountExpires) -and ($listOfBadDateValues -contains $_.AccountExpires)) {
        $_.AccountExpires = $null
    } else {
        if (([datetime]::FromFileTime($_.AccountExpires)).ToString('g') -eq $maxDateValue) {
            $_.AccountExpires = $null
        } Else {
            $_.AccountExpires = ([datetime]::FromFileTime($_.AccountExpires)).ToString('g')
        }
};$_}

How can I write the code so the two title names are changed to FirstName and LastName?

Fiddle Freak
  • 1,923
  • 5
  • 43
  • 83
  • The most straightforward way would be to construct a new object using the names you want instead of returning `$_`. See http://stackoverflow.com/questions/14012773/difference-between-psobject-hashtable-and-pscustomobject – Eris Jan 25 '16 at 19:32

1 Answers1

1

You can simply take the object and pipe it to a select statement to "alias" the property names.

$yourObject | Select-Object -Property @{N='MyNewName1';E={$_.ExistingName1}}, @{N='MyNewName2';E={$_.ExistingName2}} 
Chris N
  • 7,239
  • 1
  • 24
  • 27
  • with this way, I would have to append the objects to the table, and remove the other objects. Then re-order the columns. I think a simpler way is to just export to csv file, and use -replace on the first line. It's also not the cleanest, but I don't have to deal with making new, removing old, and re-ordering columns. I was hoping there was an easy way to access and use -replace for those two strings within the table, but I guess not. – Fiddle Freak Jan 25 '16 at 21:16
  • You can do all of those things - change values, reorder columns, etc - with Select-Object. If you want it to be easy and clean, though, you should probably import the object from a CSV and just use New-Object -Type PSObject -Property @{.... and append them to a new $collectionOfObjects. – Chris N Jan 26 '16 at 15:38