Importing/Exporting Photos to/from Active Directory

PowerShell

There are free tools like CodeTwo Active Directory Photos that lets you upload photos to Active Directory and manage them with a GUI interface. But it’s not as sleek and flexible as using a scripting tool like PowerShell.

In order to use PowerShell to communicate with Active Directory, you will need the Active Directory module installed for PowerShell. The easiest way is to install RAST (Remote Server Administration Tools) on your Windows 10 computer.

Import photos to Active Directory

To save a photo for a specific user, Get-Content of the picture in a sequence of bytes and then use Set-ADUser to replace the ThumbnailPhoto property.

$photo = [byte[]](Get-Content $photopath -Encoding byte)
Set-ADUser $username -Replace @{thumbnailPhoto=$photo}

You can name all the photos to match usernames in Active Directory’s and use the combination of Get-ADUser and Set-ADUser to import a bunch of photos at once.

$users = Get-AdUser -Filter * -SearchBase "OU=users, DC=domain, DC=local" -properties thumbnailphoto
foreach ($user in $users) {
    $photopath = "path\" + $user.samaccountname + '.jpg'
    $photo = [byte[]](Get-Content $photopath -Encoding byte)
    Set-ADUser $user -Replace @{thumbnailPhoto=$photo}
}

Export photos from Active Directory

To export the photo from a specific user, use Get-ADDUser to locate the user with a particular property named ThumbnailPhoto. Then extract the ThumbnailPhoto property and encode it to a sequence of bytes.

$user = Get-ADUser $username -Properties thumbnailPhoto
$user.thumbnailPhoto | Set-Content $photopath -Encoding byte

To export all photos attached to all users from a specific OU, you can do something similar like below:

$users = Get-AdUser -Filter * -SearchBase "OU=User, DC=domain, DC=local" -properties thumbnailphoto
foreach ($user in $users) {
    $photo = "\sharepoint\c$\inetpub\wwwroot\cisco\" + $user.samaccountname + '.jpg'
    $user.thumbnailphoto | set-content $photo -encoding byte
}

And that’s about it. It works like a charm in my case. Enjoy.

Leave a Reply

Your email address will not be published. Required fields are marked *