{"id":805,"date":"2014-08-19T00:01:00","date_gmt":"2014-08-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/08\/19\/powershell-mini-scripting-games-2014-answer-2\/"},"modified":"2014-08-19T00:01:00","modified_gmt":"2014-08-19T00:01:00","slug":"powershell-mini-scripting-games-2014-answer-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-mini-scripting-games-2014-answer-2\/","title":{"rendered":"PowerShell Mini-Scripting Games 2014: Answer 2"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Here is the answer to Problem 2 in Windows PowerShell Mini-Scripting Games 2014.<\/span>\nMicrosoft Scripting Guy, Ed Wilson, is here. Problem 2 could be a bear to solve. After all, it involves text manipulation. Last week, I posted <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2014\/08\/12\/powershell-mini-scripting-games-2014-problem-2.aspx\" target=\"_blank\">the complete Problem 2 description and task outline<\/a>.<\/p>\n<h2>Problem 2 synopsis<\/h2>\n<p>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.<\/p>\n<h3>Moving forward<\/h3>\n<p>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 <b>Select-Object<\/b> 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:<\/p>\n<p style=\"margin-left:30px\">Fname,Lname,Group,Phone,Email<\/p>\n<p style=\"margin-left:30px\">adam,adams,admin,555-1212,aa@contoso.msft<\/p>\n<p style=\"margin-left:30px\">betty,barnes,business,555-1213,bb@contoso.msft<\/p>\n<p style=\"margin-left:30px\">charlie,cassidy,customer relations,555-1214,cc@contoso.msft\nLet&#8217;s say that I need the <b>Fname<\/b>, <b>Lname<\/b>, and <b>Email<\/b> 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:<\/p>\n<p style=\"margin-left:30px\">Problem2Answer.ps1<\/p>\n<p style=\"margin-left:30px\">Import-Csv C:DataInp2.csv |<\/p>\n<p style=\"margin-left:30px\">Select-Object Fname, Lname, Email |<\/p>\n<p style=\"margin-left:30px\">Export-Csv C:DataOutp2Groomed.csv -Append -NoTypeInformation\nThe groomed data is shown here:<\/p>\n<p style=\"margin-left:30px\">&#8220;Fname&#8221;,&#8221;Lname&#8221;,&#8221;Email&#8221;<\/p>\n<p style=\"margin-left:30px\">&#8220;adam&#8221;,&#8221;adams&#8221;,&#8221;aa@contoso.msft&#8221;<\/p>\n<p style=\"margin-left:30px\">&#8220;betty&#8221;,&#8221;barnes&#8221;,&#8221;bb@contoso.msft&#8221;<\/p>\n<p style=\"margin-left:30px\">&#8220;charlie&#8221;,&#8221;cassidy&#8221;,&#8221;cc@contoso.msft&#8221;\nThat 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.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[191,169,521,3,4,45],"class_list":["post-805","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-community","tag-csv-and-other-delimited-files","tag-mini-scripting-games","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/805","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=805"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/805\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}