title: How to add tags to image with Powershell
body: <p>I have created a small powershell script to extract images from SQL server DB (stored in varbinary) to files in a server. It works perfectly! The problem is that I want to add tags to these .jpeg files based on their info in the database. I didn't find any solution on the web. Is there Powershell gurus out there, I need your help :-)</p>
  
  <p>Thanks,</p>
  
  <p>Sabrina</p>
  
  <pre><code>$SqlBH = "MYSQLQUERY"
  # Open DB Connection 
  $con = New-Object Data.SqlClient.SqlConnection;
  $con.ConnectionString = "Data Source=$Server;" +
  "Integrated Security=False;" +
  "Initial Catalog=$Database; User ID = $uid; Password = $pwd;";
  $con.Open();
  
  # New Command and Reader
  $cmd = New-Object Data.SqlClient.SqlCommand $SqlBH, $con;
  $rd = $cmd.ExecuteReader();
  
  # Create a byte array for the stream.
  $out = [array]::CreateInstance('Byte', $bufferSize)
  
  # Looping through records
  While ($rd.Read())
  {
  Write-Output ("Exporting file: {0} ParentID: {1}" -f 
  $rd.GetString(0),$rd.GetString(2));
  # New BinaryWriter
  
  # Create parent folder if not present
  $fl = $rd.GetString(2)
  $Destfl = $Dest + $fl
  $FileExists = Test-Path $Destfl
  If ($FileExists -eq $False) {new-item $Destfl -itemtype directory}
  
  $fs = New-Object System.IO.FileStream ($Destfl + $rd.GetString(0)), Create, 
  Write;
  $bw = New-Object System.IO.BinaryWriter $fs;
  
  $start = 0;
  $received = $rd.GetBytes(1, $start, $out, 0, $bufferSize - 1);
  While ($received -gt 0)
  {
  $bw.Write($out, 0, $received);
  $bw.Flush();
  $start += $received;
  $received = $rd.GetBytes(1, $start, $out, 0, $bufferSize - 1);
  }
  
  $bw.Close();
  $fs.Close();
  }
  
  # Closing &amp; Disposing all objects
  $fs.Dispose();
  $rd.Close();
  $cmd.Dispose();
  $con.Close();
  
  Write-Output ("Finished");
  </code></pre>