Bulk Upload Photos to Exchange Online

Bulk Upload Photos to Exchange Online

Be the first to vote!

Ever since the advent of DirSync, you’ve been able to add a picture’s binary content to the thumbnailPhoto attribute in AD and synchronize it to Office 365.  This blob would be rendered as a photo next to the user’s name in Outlook, OWA, and Lync.  Pretty cool, right?

That’s great in smaller environments, but what happens if you have tens or hundreds of thousands of users?  Administrators might be worried about the amount of bloat this adds to their AD DS database.  And if the DCs have small disk volumes holding ntds.dit, running out of space is a real concern.

Fortunately, you can upgrade photos directly to Exchange Online!

This script is really just a framework–there’s lots you can do with it.  It depends on a few core things:

– All photos are stored in a single directory
– All photos are named something like alias.jpg or similar format.

# Office 365 Admin Credentials
$userAdmin = "o365admin@tenant.onmicrosoft.com"
$userAdminPass = "Password123"
$securePassword = ConvertTo-SecureString $userAdminPass -AsPlainText -Force
$global:adminCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userAdmin,$securePassword
$photoPath = “C:\temp\EXOPhotos”

Import-Module MSOnline

Write-Host -ForegroundColor Green "Connecting to Office 365 ..."
$Office365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/?proxymethod=rps -Credential $adminCredential -Authentication Basic -AllowRedirection
Import-PSSession $Office365Session -AllowClobber -WarningAction SilentlyContinue -EA SilentlyContinue
Connect-MSOLService -Credential $adminCredential 

$photos = gci -Path $photoPath\*.jpg

Foreach ($file in $photos) {
       $user = $file.name.Replace(".jpg","")
       Set-UserPhoto -Identity $user -PictureData ([System.IO.File]::ReadAllBytes($file.FullName)) -Confirm:$false

Of course, this is a simple sample–any way you can figure out how to associate your images with your users will work.

Published by Aaron Guilmette

Helping companies conquer inferior technology since 1997. I spend my time developing and implementing technology solutions so people can spend less time with technology. Specialties: Active Directory and Exchange consulting and deployment, Virtualization, Disaster Recovery, Office 365, datacenter migration/consolidation, cheese.

Reader Comments

  1. I’ve been trying to get this working satisfactorily but Set-UserPhoto takes so long to upload any image it becomes unworkable for bulk imports.

    1. If you have access to an Azure VM, I would try doing it from there–copy the photos and script to an Windows 8.1 or 10 VM in Azure and then run. That way, you should avoid the general delay of the internet when running the PowerShell commands–that’s how I generally work with large datasets in O365 environments myself.

    1. That should work without issue. In this instance, since it’s not a RegEx match, the “.” character should be interpreted literally.

  2. Nice! this looks really useful, going to test it.
    Notice you are missing a space in "-Credential $adminCredential -AuthenticationBasic -AllowRedirection" it should be "-Credential $adminCredential -Authentication Basic -AllowRedirection"

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.