Summary: Here is the answer to Problem 2 in Windows PowerShell Mini-Scripting Games 2014.
Microsoft Scripting Guy, Ed Wilson, is here. Problem 2 could be a bear to solve. After all, it involves text manipulation. Last week, I posted the complete Problem 2 description and task outline.
Problem 2 synopsis
You have a CSV file that contains five columns. It comes from the HR department and it contains information that you do not need. You only need to work with three of the columns.
Moving forward
As I said, it could be a bear to solve this problem, unless you remember that when imported, a CSV file produces objects. Therefore, all I need to do is to use the Select-Object cmdlet to select the columns I need, and export the file back to a CSV file. Here is a simple CSV file that I can use:
Fname,Lname,Group,Phone,Email
adam,adams,admin,555-1212,aa@contoso.msft
betty,barnes,business,555-1213,bb@contoso.msft
charlie,cassidy,customer relations,555-1214,cc@contoso.msft
Let's say that I need the Fname, Lname, and Email from this file. All I need to do is to import the CSV file, select the three fields I want, and then export the file back out. Here is how I do it:
Problem2Answer.ps1
Import-Csv C:\DataIn\p2.csv |
Select-Object Fname, Lname, Email |
Export-Csv C:\DataOut\p2Groomed.csv -Append -NoTypeInformation
The groomed data is shown here:
"Fname","Lname","Email"
"adam","adams","aa@contoso.msft"
"betty","barnes","bb@contoso.msft"
"charlie","cassidy","cc@contoso.msft"
That is all there is to solving Problem 2. Mini-Scripting Games Answer Week will continue tomorrow when I will talk about the answer to Problem 3.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy