Digital Nervous System Making Incredible Software, Incredibly Simple

AD: Managing Photo’s

As we continue on the quest to deploy Exchange 2010, We try to encourage the team to upload photos to AD. We have even created a nice webapp to make the process very simple, but sometimes we do get the few sites come back and asked if they can bulk load the photos, or if we could report back on who has already a photo loaded.

So, I figured why not share some very simple PowerShell I use pretty often to help with the management tasks.

Import a Photo

I use the following command uses to import a photo named damian_flynn.jpg for a user named damain_flynn

import-module ActiveDirectory
$photo = [byte[]](Get-Content C:\photos\damian_flynn.jpg -Encoding byte)
Set-ADUser damian_flynn -Replace @{thumbnailPhoto=$photo}

Export a Photo

In a similar manner, I can also extract the photo from Active Directory

$user = Get-ADUser damian_flynn -Properties thumbnailphoto
$user.thumbnailphoto | Set-Content c:\photos\extract\damian_flynn.jpg -Encoding byte

Check for a Photo

Of course we do have to run a report some times, to see which users have already imported a photo. So using the same process as extracting a photo, I just add a simple If check to generate the result

Import-module ActiveDirectory
$result = get-aduser damian_flynn -Properties thumbnailphoto;
if ($result.thumbnailphoto) {
  Write-Host "$($result.Name) - Photo Loaded!"

Batch Jobs

Of course, we normally need to run batches of users to get a full report, so most often we do this with a CSV list of the users, and a quick loop.

As an example, I will create a CSV file, with a heading, and then each user name


Now, in PowerShell we can import the CSV file into an array, which I am calling $userlist.

I then run a Foreach loop against each user in the list, updating the variable on each pass of the loop.

Finally, then use the variable username from the column heading in the CSV file as the identity for the Get-ADUser command

Import-module ActiveDirectory
$userlist = Import-Csv ./users.csv
foreach ($account in $userlist) {
  $result = get-aduser $account.username -Properties thumbnailphoto;
  if ($result.thumbnailphoto) {
    Write-Host "$($result.Name) - Photo Loaded!"

So, PowerShell rocks again.

Be social and share this post!
Share via OneNote