Bulk Upload Photos to Exchange Online


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

 

Function o365Logon()

       {

       # Connect to Office 365 Environment

       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

       }

      

o365Logon

 

$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

       }

Comments (7)

  1. John Bryntze says:

    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"

  2. Thanks, John! I must have taken out the space when I was trying to fit it on to the screen. Great catch!

  3. Linda says:

    Hi Aaron,

    What if the alias contains a “.” such as “firstname.lastname”

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

  4. Mark Salter says:

    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.

Skip to main content