Weekend Scripter: Exporting and Importing Photos in Active Directory

Doctor Scripto

Summary: Use Windows PowerShell to import and export photos in Active Directory.

Honorary Scripting Guy, Sean Kearney, is here. I was playing about with Active Directory this week. I wanted to be able to get photos to automatically populate on my workstations running Windows 7.

By doing some online reading, I found that you can store this in the ThumbnailPhoto attribute within Active Directory.

In truth this that is not a hidden feature. If you're running Exchange Server 2013, Exchange Server 2010, or Lync, there are already cmdlets to do this.

But I wanted to play. I actually wanted two options on my plate. (What can I say…I'm greedy!)

I wanted the ability to import and export the pictures to and from Active Directory. Yes, I know. I'm just mad about the power from Windows PowerShell!

So what do I need?

  • A picture in .jpg format and no larger than 100 KB in size.
  • Microsoft Active Directory
  • Windows PowerShell
  • 100 pounds of baked beans and a panda

OK, fine. I was completely lying about the last bullet. I wanted to see if you were reading.

So we're going to work with the Microsoft Active Directory cmdlets, although the process for the Quest cmdlets is very similar.

First off, I'm going to start with a simple .jpg file. I swiped one from my Twitter account:

Photo of Sean Kearney

I used Microsoft Paint to drop the resolution to 52 x 52 pixels while I was playing. So it's really small, but you're supposed to be able to upload a photo of 100 KB in size.

So the file is sitting on my hard drive in a little folder called C:\Photos, and the file is named Sean.jpg.

To bring in the file, I have to use a little .NET to read the file:

[SYSTEM.IO.FILE]::ReadAllBytes()

We're going to read the JPG file as a binary stream because that's what the Active Directory ThumbnailPhoto attribute needs. So let's add the attribute to this "Sean guy:"

$Picture=[System.IO.File]::ReadAllBytes('C:\Photos\sean.jpg')

SET-ADUser SeanK –add @{thumbnailphoto=$Picture}

If you had this information in Active Directory and you need to export it (for any reason at all), we can do that too.

First we grab the User object from Active Directory:

$User=GET-ADUser SeanK –properties thumbnailphoto

Then we export the information with a little more .NET magic:

$Filename='C:\Photos\Export.jpg'

[System.Io.File]::WriteAllBytes($Filename, $User.Thumbnailphoto)

Pretty cool, eh?

We could even go so far as to export all the photos in Active Directory to the file system:

$list=GET-ADuser –filter * -properties thumbnailphoto

Foreach ($User in $list)

{

$Directory='C:\Photos\'

If ($User.thumbnailphoto)

  {

  $Filename=$Directory+$User.samaccountname+'.jpg'

  [System.Io.File]::WriteAllBytes($Filename, $User.Thumbnailphoto)

  }

}

If you aren't running Exchange Server 2013, Exchange Server 2010, or Lync, and you would like to play with this idea further, there's an excellent article and solution written by a fellow in the community named Jimmy Mathew. He created a really cool solution to allow you to set the photo by using the stored picture in Active Directory: Windows 8/Windows 8.1: Set Account Picture from Active Directory.

Now let's get going! Start populating and truly personalizing your Active Directory environment!

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send an email to the Scripting Guys at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, remember, the Power of Shell is in You.

Sean Kearney, Windows PowerShell MVP, Honorary Scripting Guy

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Pedro Roeseler 0

    Thank you so much for this info! I was getting crazy with the flow problems of the user photos on our hybrid solution (On prem AD + Azure+Office 365) and this definitly helped a lot to find the root cause!

Feedback usabilla icon