Friday, 9 August 2013

Add or Update a User Profile Picture (Thumbnail) in Active Directory using PowerShell with One Line of Code

This is a quick post to show how easy it is to update an Active Directory user account with a photo of the user (actually, it could be a photo of a cow if you like, but that's beside the point!).

Before you do this, you need to have the Active Directory module for PowerShell installed. You can find out about installing and using the Active Directory module for PowerShell on Microsoft's TechNet site, here:

Active Directory Administration with Windows PowerShell
Active Directory Cmdlets in Windows PowerShell

Note that there are constraints on the sizes and formats supported for the thumbnailPhoto Active Directory attribute. As a general guideline, keep your images to 96x96 pixels and under 10Kb.

Now, for the fun bit! Let's assume we have user Crusoe, and we have saved Crusoe's photo to C:\temp\crusoe.jpg

In two lines of code, we can update his photo.

Get the photo, using the Get-Content PowerShell cmdlet, using the encoding type byte. Store the photo as a byte array in the $photo variable. Then update Active Directory using the Set-ADUser cmdlet, passing the byte array ($photo) to the thumbnailPhoto attribute.
$photo = [byte[]](Get-Content "C:\temp\crusoe.jpg" -Encoding byte)
Set-ADUser Crusoe -Replace @{thumbnailPhoto=$photo

To shorten this to one line of code, we could write this as;
Set-ADUser Crusoe -Replace @{thumbnailPhoto=([byte[]](Get-Content "C:\temp\crusoe.jpg" -Encoding byte))}

Wholla! Easy hey?!

1 comment: