Thursday, April 19, 2012

Bulk import thumbnail photos

In these days of increasing messaging and collaboration using such excellent services as Exchange, Lync and Sharepoint, the need to import thumbnail photos into the Active Directory is in high demand. There are many options to perform this task  but I prefer the one tool that will eventually rule them all, PowerShell. Gather all user pictures and place them in a folder with the following prerequisites.

  • The files must have names that corresponds to unique user attributes such as SamAccountName
  • The files must not exceed 10kb in size
  • The dimensions of the pictures should be the recommended 96x96 pixels but this isn't required
  • All files must be JPEG images (*.jpg)

Once the folder with above mentioned prerequisites have been established, the following script imports them to the Active Directory.

Path = 'C:\Temp\ImportThumbnails\Photos'
import-module ActiveDirectory
ForEach ($File in Get-ChildItem $Path | Where-Object { $_.Extension -eq ".jpg" } )
{
  $UserName = $File.Name.substring(0, $File.Name.Length - 4)
  Write-Host $UserName -NoNewLine
  $Photo = [byte[]](Get-Content -Path $File.Fullname -Encoding byte)
  Set-ADUser $UserName -Replace @{thumbnailPhoto=$Photo}
  Write-Host " [Done]" -ForeGroundColor Green
}

Don't forget to make sure that the thumbnailPhoto attribute is replicated to the Global Catalog in the Active Directory Schema.

No comments:

Post a Comment