I have a csv that will need to be scripted to update monthly it currently looks like this
UserID Name
120 john doe
121 jane doe
122 mike name
123 joe blow
I need to keep the existing UserID and add the UserID data before the Name and delete the last name.
UserID ID
120 120-john
121 121-jane
122 122-mike
123 123-joe
I have figured out renaming removing columns but I have no idea how to combine data from one to the other and remove the last name....
any help would be amazing! even if you can just fill in part of the issue I can keep playing
This can be done easily by importing the csv and passing it to Select-Object with a calculated property...
Import-Csv .\Input.csv | Select-Object UserID, @{n="ID";e={$_.UserID + "-" + $_.Name.Split(" ")[0]}}
And Pipe to Export-Csv if you want to save the output...
Import-Csv .\Import.csv | Select-Object UserID, @{n="ID";e={$_.UserID + "-" + $_.Name.Split(" ")[0]}} | Export-Csv -NoTypeInformation Output.csv
So what's going on here? In the second part of my select statement, (the @{..} part) :
@{n="ID";e={$_.UserID + "-" + $_.Name.Split(" ")[0]}}
.. I'm saying, the name (n=) of the column is "ID", and the value is determined by the expression (e=), which is between the next pair of braces.
And that expression is saying, take the UserID ($ .UserID), plus a hyphen and append the first part of my $ .Name column (split by spaces).
Hope that helps (and made sense), Cheers :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.