I am often given a CSV and asked to add information to it. For a simplified example I may be handed the following CSV and asked to get the lastlogontimestamp for each user
In order to achieve this I did the following
$csv = import-csv users.csv
$report = "User Audit_{0:dd-MM-yyyy_HHmm}.csv" -f (Get-Date)
$csv | ForEach-Object {
$user = ""
$user = get-aduser $_.user -Properties lastlogontimestamp | Select-Object @{ N = 'LastLogonTimestamp'; E = { [DateTime]::FromFileTime($_.LastLogonTimestamp) } }
[pscustomobject]@{
User = $_.User
'First Name' = $_.'First Name'
'Last Name' = $_.'Last Name'
'LastLogonTimestamp (AD)' = $user.lastlogontimestamp
} | Export-Csv $report -NoTypeInformation -Append
}
This works very well but creating it can be time consuming for spreadsheets with lots of columns and is subject to human error during the pscustomobject construction. So my question is - rather than manually constructing the pscustomobject is there a way to code it (using the column headings to populate each key and value), after which I can append my new additions?
