Active Directory: Add or Update a User Picture using PowerShell
Introduction
This is a quick article to show how easy it is to update an Active Directory user account with a photo of the user.
The Active Directory thumbnailPhoto attribute is used by several applications to display a picture for the user account. Microsoft Outlook is one such application that uses this attribute to display the picture of people you send and receive emails to and from (within an Active Directory domain).
Before using the PowerShell commands below, 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 the Microsoft TechNet site, here:
Active Directory Administration with Windows PowerShell
Active Directory Cmdlets in Windows PowerShell
Also 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.
Example
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 Crusoe's 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))}
References
- This article originally came from a post of Matthew Yarlett which can be found here, Add or Update a User Profile Picture (Thumbnail) in Active Directory using PowerShell with One Line of Code