title: Powershell - Encoding to export AD thumbnailPhoto
body: <p>I use a powershell script to export the active directory thumbnailPhoto attribute to a sql server table.
  The goal is to display the photo using asp.net.</p>
  
  <p>The problem I have is that I convert it to a string (column in tsql is nvarchar) which then gives me RGB values. Not using string just gives me "System.Byte[]"</p>
  
  <p>The values are stored like this:</p>
  
  <pre><code>255 216 255 225 0 24 69 120 105 102...
  </code></pre>
  
  <p>instead of what I was hoping:</p>
  
  <pre><code>ffd8ffe1001845786966000049492a...
  </code></pre>
  
  <p>This is a part of my script:</p>
  
  <pre><code>ForEach ($user in $users) {
  $tempUser = New-Object PSObject -Property @{ sAMAccountName=""; thumbnail="" }
  $tempUser.thumbnail = $user.thumbnailPhoto
  }
  ....
  $datatable = New-Object System.Data.Datatable "insert"
  $datatable.Columns.Add("thumbnail") | Out-Null
  ForEach ($user in $SQLusers) {
      $row = $datatable.NewRow()
  $row["thumbnail"] = [String]$user.thumbnail
  $datatable.Rows.Add($row)
  }
  </code></pre>
  
  <p>So my question drifts into two directions:</p>
  
  <ul>
  <li><p>how can I display above RGB values with asp.net?</p></li>
  <li><p>or how can I transform the RGB values to get the correct byte encoding?</p></li>
  </ul>
  
  <p>Thank you.</p>