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?

1
  • The most straightforward way would be to construct a new object using the names you want instead of returning $_. See stackoverflow.com/questions/14012773/… Commented Jan 25, 2016 at 19:32

1 Answer 1

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}} 
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.