2

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

2
  • are you're saying the first example (w/ UserID,Name) is your input file? .. and the second example (w/ UserID,ID) is what you want the output to look like? Commented Jan 5, 2018 at 6:28
  • yes that's it exactly sorry if i wasn't clear Commented Jan 5, 2018 at 6:33

1 Answer 1

3

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 :)

Sign up to request clarification or add additional context in comments.

Comments

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.