{"id":12211,"date":"2011-11-02T00:01:00","date_gmt":"2011-11-02T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/11\/02\/remove-unwanted-quotation-marks-from-csv-files-by-using-powershell\/"},"modified":"2011-11-02T00:01:00","modified_gmt":"2011-11-02T00:01:00","slug":"remove-unwanted-quotation-marks-from-csv-files-by-using-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/remove-unwanted-quotation-marks-from-csv-files-by-using-powershell\/","title":{"rendered":"Remove Unwanted Quotation Marks from CSV Files by Using PowerShell"},"content":{"rendered":"<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\"><strong>Summary: <\/strong>Learn how to remove unwanted quotation marks from a CSV file by using Windows PowerShell.<\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">&nbsp;<\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333;font-family: Segoe\">Microsoft Scripting Guy Ed Wilson here. The fall is rapidly falling down here in Charlotte, North Carolina, in the United States. It is cooling off here, and is around 60 degrees Fahrenheit (15.5 degrees Celsius, according to my <\/span><a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=conversion%20module&amp;sections=7618\"><span style=\"font-family: Segoe\">conversion module<\/span><\/a><span><span style=\"color: #333333\"><span style=\"font-family: Segoe\">). The neighbors have all been wracked with anguish over whether to tough it out for a few more days, or to go ahead and turn on the heater. It is, evidently, such a convoluted decision that they have made numerous Facebook postings about it. But I guess I would rather read about anguished decisions about to turn on the heater or not, than to read about the latest nonferrous material that their pet consumed.&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333;font-family: Segoe\">The Internet is cool. For example, I really enjoy comments that are posted on the Hey, Scripting Guy! Blog. In fact, I have subscribed to a RSS feed that alerts me any time that a new comment is made, regardless of how old the article may be. Just this morning, I replied to two comments from readers on postings that were more than five years old; that&rsquo;s <\/span><a href=\"http:\/\/en.wikipedia.org\/wiki\/List_of_unusual_units_of_measurement#Dog_year\"><span style=\"font-family: Segoe\">35 in doggy years<\/span><\/a><span><span style=\"color: #333333\"><span style=\"font-family: Segoe\">, and more like 50 in Internet years. <\/span><\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333;font-family: Segoe\">Anyway, the other day, I wrote a pretty cool article named, <\/span><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/10\/31\/use-powershell-to-append-csv-files-easily.aspx\"><span style=\"font-family: Segoe\">Use PowerShell to Append CSV files Easily<\/span><\/a><span style=\"color: #333333;font-family: Segoe\">.In the comments about that article, Jeffrey S. Patton from Kansas (yes <\/span><a href=\"http:\/\/en.wikipedia.org\/wiki\/The_Wizard_of_Oz_(1939_film)\"><span style=\"font-family: Segoe\">Toto, he is from Kansas<\/span><\/a><span><span style=\"color: #333333\"><span style=\"font-family: Segoe\">) posted a asked why I used Notepad<i> <\/i>to remove commas instead of programmatically replacing the commas in the output with Windows PowerShell. I answered that I did it because opening the file in Notepad, clicking <b>Edit<\/b>, and then clicking <b>Replace<\/b> was faster and easier than writing the extra code to replace the quotation marks. In a one-off scenario, that is probably true. But what if this is something that needs to be scheduled on a regular basis? If it is an automation scenario, opening the file in Notepad<i> <\/i>is off the table; as cool as Notepad<i> <\/i>is, it does not have an automation interface, so it is not a scriptable solution. <\/span><\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333;font-family: Segoe\">What exactly is the problem? When using the <b>Export-CSV<\/b> cmdlet, it automatically adds quotation marks around everything. In some cases, when reading this file, the quotation marks could be interpreted as part of the data, which of course would be a problem. It is rare, but it most certainly could happen. In fact, this very problem is one reason I quit using Microsoft Excel to manipulate CSV files: it has the same &ldquo;feature.&rdquo; This happens because if you have a space in a column, some programs are not smart enough to respect the space between commas. Therefore, to help prevent an error with a column that has a space in it, quotation marks are added everywhere. The output file produced <\/span><span style=\"color: #333333;font-family: Segoe\">from <\/span><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/11\/01\/use-powershell-to-remove-duplicate-lines-from-a-csv-file.aspx\"><span style=\"font-family: Segoe\">our command from yesterday<\/span><\/a><span style=\"color: #333333\"><span style=\"font-family: Segoe\"> (shown here) is shown in the figure following this code:<\/span><\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">dir c:\\fso -Filter *.csv | ? {$_.basename -like &#8216;users?&#8217;} | Import-Csv | sort lname,fname | Export-Csv -Path c:\\fso\\UsersConsolidated.csv &ndash;NoTypeInformation<\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7024.hsg-11-2-11-01.png\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of output file with quotation marks\" alt=\"Image of output file with quotation marks\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7024.hsg-11-2-11-01.png\" width=\"495\" height=\"510\" \/><\/a><\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">Jeffrey was absolutely correct when he said I could use <b>replace<\/b><i> <\/i>to remove the quotation marks from the output file. It is actually pretty simple. <\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">Here are the steps I need to perform:<\/span><\/span><\/span><\/p>\n<ol>\n<li><span style=\"color: #333333;font-size: small\"><span><span style=\"font-family: Segoe\">Use the <b>Get-Content<\/b> cmdlet to read the text of the usersconsolidated.csv<i> <\/i>file. <\/span><\/span><\/span><\/li>\n<li><span style=\"color: #333333;font-size: small\"><span><span style=\"font-family: Segoe\">Use the <b>Foreach-Object<\/b> cmdlet (<b>%<\/b> is an alias) to read each line as it comes from the file.<\/span><\/span><\/span><\/li>\n<li><span style=\"color: #333333;font-size: small\"><span><span style=\"font-family: Segoe\">Inside the script block for the <b>Foreach-Object<\/b> command, use the <b>$_ <\/b>automatic variable to reference the current line and the <b>replace<\/b><i> <\/i>operator to replace a quotation mark with nothing.<\/span><\/span><\/span><\/li>\n<li><span style=\"color: #333333;font-size: small\"><span><span style=\"font-family: Segoe\">Use the <b>Out-File<\/b> cmdlet to overwrite the existing usersconsolidated.csv<i> <\/i>file with the newly changed content that no longer contains the quotation marks.<\/span><\/span><\/span><\/li>\n<li><span style=\"color: #333333;font-size: small\"><span><span style=\"font-family: Segoe\">Use the <i>force <\/i>switched parameter to tell the <b>Out-File<\/b> cmdlet to overwrite existing content.<\/span><\/span><\/span><\/li>\n<li><span style=\"color: #333333;font-size: small\"><span><span style=\"font-family: Segoe\">Use the <i>encoding <\/i>parameter to specify ASCII<i> <\/i>encoding to maintain compatibility with legacy applications.<\/span><\/span><\/span><\/li>\n<\/ol>\n<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">The command line I use is much simpler than the six steps above would make it seem. Here is the command:<\/span><\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">(Get-Content C:\\fso\\UsersConsolidated.csv) | % {$_ -replace &#8216;&#8221;&#8216;, &#8220;&#8221;} | out-file -FilePath C:\\fso\\UsersConsolidated.csv -Force -Encoding ascii<\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">The preceding command is a single-line command that has wrapped. No line continuation marks are used in the command. The parentheses around the <b>Get-Content<\/b> cmdlet are required (otherwise, it creates a blank file). I can actually shorten this command by searching for additional aliases. I use the <b>Get-Alias<\/b> cmdlet to search for aliases. The cool thing is that it will accept an array for input, and therefore I can look for aliases for both <b>Get-Content<\/b> and <b>Out-File<\/b> at the same time. Here is the command:<\/span><\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">Get-Alias -Definition get-content, out-file<\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">The bad thing about the <b>Get-Alias<\/b> cmdlet is that it returns an error when no match appears. This behavior is shown in the following figure.<\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0777.hsg-11-2-11-02.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image showing error when no match appears\" alt=\"Image showing error when no match appears\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0777.hsg-11-2-11-02.png\" \/><\/a><\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">The shortened version of the command uses the <b>gc<\/b><i> <\/i>alias for the <b>Get-Content<\/b> cmdlet. It also uses positional parameters and partial parameter names. <\/span><\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">(gc C:\\fso\\UsersConsolidated.csv) | % {$_ -replace &#8216;&#8221;&#8216;, &#8220;&#8221;} | out-file C:\\fso\\UsersConsolidated.csv -Fo -En ascii<\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">As shown in the following figure, there are no more quotation marks in the file. <\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\"><\/span><\/span><\/span><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8322.hsg-11-2-11-03.png\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of output file with no quotation marks\" alt=\"Image of output file with no quotation marks\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8322.hsg-11-2-11-03.png\" width=\"472\" height=\"645\" \/><\/a><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">If you want to do this all in a single command, it is easier to switch to using the <b>ConvertTo-CSV<\/b> cmdlet instead of the <b>Export-CSV<\/b> cmdlet. The reason is that <b>ConvertTo-CSV<\/b> converts to CSV format, but does not export to a file. This allows time to replace the quotation marks prior to writing to file. The revised command is shown here:<\/span><\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">dir c:\\fso -Filter *.csv | ? {$_.basename -like &#8216;users?&#8217;} | Import-Csv | &nbsp;sort lname,fname | convertto-csv -NoTypeInformation | % { $_ -replace &#8216;&#8221;&#8216;, &#8220;&#8221;} | out-file c:\\fso\\usersconsolidated.csv -fo -en ascii<\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">There is no alias for the <b>ConvertTo-CSV<\/b> cmdlet, but there is an alias for <b>Import-CSV<\/b>, which is <b>ipcsv<\/b>. I can also use <b>ls<\/b> as an alias for <b>Get-ChildItem<\/b>, instead of the lengthier <b>dir<\/b> alias. I can also shorten <b>filter<\/b><i> <\/i>to <b><i>fi<\/i><\/b><i> <\/i>(I cannot use the single letter <b>f <\/b>because there is also a parameter <i>force <\/i>defined for the <b>Get-ChildItem<\/b> cmdlet). Here is the shortened command:<\/span><\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">ls c:\\fso -Fi *.csv | ? {$_.basename -like &#8216;users?&#8217;} | ipcsv | sort lname,fname | convertto-csv -NoTypeInformation | % { $_ -replace &#8216;&#8221;&#8216;, &#8220;&#8221;} | out-file c:\\fso\\usersconsolidated.csv -fo -en ascii<\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\">Well, that is about all there is to removing quotation marks from a CSV file. Join me tomorrow for more Windows PowerShell fun. <\/span><\/span><\/span><\/p>\n<p><span style=\"color: #333333;font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333;font-family: Segoe\">I invite you to follow me on <\/span><a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><span style=\"font-family: Segoe\">Twitter<\/span><\/a><span style=\"color: #333333;font-family: Segoe\"> and <\/span><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span style=\"font-family: Segoe\">Facebook<\/span><\/a><span style=\"color: #333333;font-family: Segoe\">. If you have any questions, send email to me at <\/span><a href=\"mailto:scripter@microsoft.com\" target=\"_blank\"><span style=\"font-family: Segoe\">scripter@microsoft.com<\/span><\/a><span style=\"color: #333333;font-family: Segoe\">, or post your questions on the <\/span><a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><span style=\"font-family: Segoe\">Official Scripting Guys Forum<\/span><\/a><span><span style=\"color: #333333\"><span style=\"font-family: Segoe\">. See you tomorrow. Until then, peace.<\/span><\/span><\/span><\/span><\/p>\n<\/p>\n<p><span style=\"font-size: small\"><b><span style=\"color: #333333\">Ed Wilson, Microsoft Scripting Guy<\/span><\/b><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"color: #333333\"><span style=\"font-family: Segoe\"><\/span><\/span><\/span>&nbsp;<\/p>\n<p><span style=\"color: #333333;font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to remove unwanted quotation marks from a CSV file by using Windows PowerShell. &nbsp; Microsoft Scripting Guy Ed Wilson here. The fall is rapidly falling down here in Charlotte, North Carolina, in the United States. It is cooling off here, and is around 60 degrees Fahrenheit (15.5 degrees Celsius, according to my [&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":[169,3,4,45],"class_list":["post-12211","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-csv-and-other-delimited-files","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to remove unwanted quotation marks from a CSV file by using Windows PowerShell. &nbsp; Microsoft Scripting Guy Ed Wilson here. The fall is rapidly falling down here in Charlotte, North Carolina, in the United States. It is cooling off here, and is around 60 degrees Fahrenheit (15.5 degrees Celsius, according to my [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12211","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=12211"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12211\/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=12211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}