Freigeben über


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

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.

Comments

  • Anonymous
    October 18, 2015
    cool
    thanks
  • Anonymous
    October 21, 2015
    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"
  • Anonymous
    October 22, 2015
    Thanks, John! I must have taken out the space when I was trying to fit it on to the screen. Great catch!
  • Anonymous
    March 17, 2017
    Hi Aaron,What if the alias contains a "." such as "firstname.lastname"
    • Anonymous
      April 04, 2017
      That should work without issue. In this instance, since it's not a RegEx match, the "." character should be interpreted literally.
  • Anonymous
    August 10, 2017
    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.
    • Anonymous
      August 17, 2017
      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.