title: Export Unicode characters from SQL to CSV via PowerShell
body: <p>How do I export Unicode-y characters to CSV in PowerShell? Seems like <code>-Encoding UTF8</code> should do the trick.  I have the following object in PowerShell:</p>
  
  <pre><code>$data = @{"column" = "1/13/14-&gt;1/26/14"}
  </code></pre>
  
  <p>What I've actually got is a single-character unicode right-arrow coming from a SQL query, not the dash-greater-than symbols I've typed in above, but I can't seem to copy-paste the right-arrow into the Stack Overflow editor.  I believe the arrow character is successfully getting from SQL to PowerShell because I can log it to the PowerShell console:</p>
  
  <p><img src="https://i.stack.imgur.com/v7WPK.jpg" alt="enter image description here"></p>
  
  <p>Anyway, I want to export that object to CSV:</p>
  
  <pre><code>$table = $data.getEnumerator() | foreach{
    New-Object PSObject -Property ([ordered]@{column = $_.Value})
  }
  $table | export-csv export.csv -NoTypeInformation -Encoding UTF8
  </code></pre>
  
  <p>Problem is, the right-arrow fails to encode properly. Looking at the CSV in Notepad++, I see the too-familiar black <code>SUB</code> box where the arrow should be.</p>
  
  <p><img src="https://i.stack.imgur.com/6M6p1.jpg" alt="enter image description here"></p>
  
  <p>I've tried all the Export-CSV encodings listed on Microsoft's <a href="https://technet.microsoft.com/en-us/library/hh849932.aspx" rel="nofollow noreferrer">Export-CSV page</a>, to no avail. What am I missing about CSV and encoding here?  This sort of low-level data handling should be right in PowerShell's wheelhouse.  Is it possible that I need to do some kind of additional encoding management in the SQL -> PowerShell step?  There I'm using very straightforward ADO.Net:</p>
  
  <pre><code>function Invoke-Sql {
  param(
      [string] $dataSource = ".\SQLEXPRESS",
      [string] $database = "MasterData",
      [string] $sqlCommand = $(throw "Please specify a query.")
    )
  
    $connectionString = "Data Source=$dataSource; " +
          "Integrated Security=true; " +
          "Initial Catalog=$database"
  
    $connection = New-Object System.Data.SqlClient.SQLConnection($connectionString)
    $command = New-Object System.Data.SqlClient.SQLCommand($sqlCommand,$connection)
    $connection.Open()
  
    $adapter = New-Object System.Data.SqlClient.SqlDataAdapter $command
    $dataset = New-Object System.Data.DataSet
    $adapter.Fill($dataSet) | Out-Null
  
    $connection.Close()
    return $dataSet   
  }
  </code></pre>