title: Trying to export an array to csv using powershell (array objects are ; delimited and contain newlines)
body: <p>Okay, so I have this array, which is essentially an <code>Import-Csv</code> of a CSV file.  Each object in the array is an entire CSV row.  The input file renders properly when I open it with Excel (The columns are correct)</p>
  
  <p>This CSV file is comma delimited, in the following example format:</p>
  
  <blockquote>
    <p>"first","second","this is all</p>
    
    <p>the third</p>
    
    <p>column","fourth"</p>
  </blockquote>
  
  <p>I have gotten everything in my script to work properly, except for the part where I need to write this array to a .csv file in a format which will properly render.  I have been having issues using <code>Out-File</code>, <code>Export-Csv</code>, and I even tried <a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/09/23/use-powershell-to-work-with-csv-formatted-text.aspx" rel="nofollow">this</a> but it doesn't give me exactly what I need.  Each object needs to be a new line in the csv file, appear only once, and must adhere to Excel's rendering of CSV files (i.e. each column needs to be double quoted and comma delimited).  </p>
  
  <p>I even got so desperate that I'm trying to manually format this thing correctly by doing this:</p>
  
  <pre><code>$Array | % { $_ -replace "`"@`{","`n`"" } | % { $_ -replace "}`"","`"`n" } | % { $_ -replace "`;","`"`;`"" } | % { $_ -replace "Plugin ID=","" } | % { $_ -replace "CVE=","" } | % { $_ -replace "CVSS=","" } | % { $_ -replace "Risk=","" } | % { $_ -replace "Host=","" } | % { $_ -replace "Protocol=","" } | % { $_ -replace "Port=","" } | % { $_ -replace "Name=","" } | % { $_ -replace "Synopsis=","" } | % { $_ -replace "Solution=","" } | % { $_ -replace "Description=","" } | % { $_ -replace "Plugin Output=","" } | % { $_ -replace "`";`"","`",`"" } | Out-File "$dirResults\$Results-$Time.csv"    
  </code></pre>
  
  <p>Essentially, what I'm doing here is a couple of things:</p>
  
  <ul>
  <li>Remove @{ from the beginning and } from the end of each object</li>
  <li>Change the delimeter from ; to ,</li>
  <li>Put each column in double quotations</li>
  <li>Remove the ColumnHeader= from the beginning of each value</li>
  </ul>
  
  <p>Here is a snippet of code:</p>
  
  <pre><code>$arrayHeader = @("Plugin ID;CVE;CVSS;Risk;Host;Protocol;Port;Name;Synopsis;Description;Solution;Plugin Output")
  $OutterArrayException = Import-Csv "$dirExceptions\$FileException" -Delimiter ","
  $InnerArrayScanMe = New-Object System.Collections.ArrayList
  # This method keeps the arrays separate types
  FOREACH ( $Object in $OutterArrayScanMe )
          {   $InnerArrayScanMe.Add("$Object") | Out-Null }
  # Note that the OutterArrayException is an Import-Csv from a similarly formatted file as OutterArrayScanMe
  FOREACH ( $Exception in $OutterArrayException )
          { $InnerArrayScanMe.Remove("$Exception") }
  # This method keeps the arrays separate types
  $OutterArrayScanMe = @($arrayHeader) + @($InnerArrayScanMe)
  $OutterArrayScanMe | Export-Csv "$dirResults\$FileScanMe-$startTime.csv" -Delimiter ";" -NoTypeInformation
  </code></pre>
  
  <p><strong>UPDATE</strong>:<br>
  When I use export-csv, the new lines for an object place the information as a new row in the csv.  Thus, something that should look like this gets really messy.  </p>
  
  <p><strong>UPDATE2</strong>:<br>
  There have been some questions about what the format looks like and what the issues are with the format.  To clear those up, I have included a single object in the array which demonstrates the issue.  Remember, what happens in my script is there is an import-csv of a properly formatted CSV (Has multi line, etc. and renders perfectly), removal of unnecessary objects (using $array.Remove), and then a dump to file (Out-File or Export-Csv).  So simple, yet so broken...</p>
  
  <p><strong>Example Array Object</strong> (Out-File):<br>
  @{Plugin ID=#####; CVE=CVE-####-####; CVSS=#.#; Risk=Medium; Host=###.###.###.###; Protocol=xxx; Port=##; Name=Microsoft ASP.NET xxx; Synopsis=A framework used by the remote web server<br>
  vulnerability.; Description=The web server running on the remote host appears to be using Microsoft<br>
  ASP.NET, and may be affected by a vulnerability.   </p>
  
  <p>Additionally, there is speculation that this vulnerability could result
  in unicorns and rainbows if an attacker with physical access to the machine
  plugs in.; Solution=Use an magic filter to block requests to naughty sites.; Plugin Output=
  Nessus received an naughty page or related error message by requesting<br>
  the following URL :  </p>
  
  <p>hxxp://###.###.###.###/xxx/xxxxxxxxx<br>
  }  </p>
  
  <p><strong>Example Array Object</strong> (Export-Csv):<br>
  #TYPE System.String<br>
  "Length"<br>
  "616"</p>