I'm editing a CSV into something more usable and I'm stuck at making a new column. This is what I have so far (I'm sure there is a better way to write this but I'm new to PowerShell).
$temp = 'C:\test\temp.csv'
Import-Csv $temp | Select-Object @{e={$_.'Employee EIN'};l='location'},
@{e={$_.'Employee Status'};l='status'},
@{e={$_.'Employee Id'};l='badge' },
@{e={$_.'Last Name'};l='lastname'},
@{e={$_.'First Name'};l='firstname'},
@{e={$_.'Date Terminated'};l='termdate'},
@{e={$_.'Jobs (HR)(1)'};l='department'},
@{e={$_.'Supervisor Name'};l='supervisor'} | Export-Csv -NoTypeInformation 'C:\test\data.csv'
I need to combine the first letter from the firstname and lastname columns, as well as the badge column, and put that data into a new column called employeeid. Is this possible, and how would I go about this? Thanks
I got stuck because I don't know how to combine the data I need into a new column
CodePudding user response:
You can use the index operator [ ]
to get the index 0
(first character) of the First Name
and Last Name
properties. Then you can use the format operator -f
to do the string interpolation. I would personally use pscustomobject
to instantiate your objects, the syntax is much easier on the eyes:
$temp = 'C:\test\temp.csv'
Import-Csv $temp | ForEach-Object {
[pscustomobject]@{
location = $_.'Employee EIN'
status = $_.'Employee Status'
badge = $_.'Employee Id'
lastname = $_.'Last Name'
firstname = $_.'First Name'
termdate = $_.'Date Terminated'
department = $_.'Jobs (HR)'
supervisor = $_.'Supervisor Name'
employeeId = '{0}{1}{2}' -f $_.'First Name'[0], $_.'Last Name'[0], $_.'Employee Id'
}
} | Export-Csv -NoTypeInformation 'C:\test\data.csv'