Download users photos from Exchange Online with PowerShell

One of the great benefits of Office 365 is how users profiles can be customized and filled with helpful information. Having appropriate manager attributes, for example, can provide an out of the box org chart that can be very useful in large organizations. Users can list their skills or interests on their profile and and this can be made available to the wider organization to help bring people who may never have met in person together with information about each other.

Another useful piece of functionality is the simple profile picture. I don’t know how many times I’ve been looking for someone new in our offices and just knowing what they look like has made life a lot easier. This is particularly useful for new starters who have a lot of new faces to get used to. This isn’t a new idea and profile pictures in Exchange / Active Directory and Exchange Online have been around for more than a decade.

As I’ve mentioned in other posts, my team and I carry out a lot of Office 365 migrations (mergers or acquisitions mostly) and I’m always looking to enhance the end user experience. With some simple scripting we can take what is a best a disjointed experience and start enhancing it by removing some of the small things that remind users that there has been a change. One of these small things is, with most tenant to tenant migrations, user photos aren’t migrated by the standard migration tools that are available. Usually things like this are handled through communication and users can upload their photos again after migration but it does have an impact on that day 1 experience.

Another time downloading photos can be a requirement is when they need to be uploaded to other systems, like for instance, a HR system. It’s annoying to have two different areas where photos can live to represent users and they are often managed by two different teams.

To facilitate downloading photos from Office 365, I’ve put together a simple PowerShell script. The script uses the Exchange Online management shell and saves all user mailbox photos to your local machine in the folder C:\temp\photos

Connect-ExchangeOnline

##Get all user mailboxes
$users = Get-user -ResultSize unlimited -RecipientTypeDetails usermailbox

##Reset Progress Counter
$i=0

##Start Loop
foreach($mailbox in $users){
##Iterate Progress
$i++
##Write Progress
write-host "$i out of $($users.count): $($mailbox.windowsemailaddress)"

##Start Try
try{

##Nullify Current User
$user = $null

##Get Photo
$user = Get-UserPhoto $mailbox.identity -erroraction stop

##If successful
if($user){

##Export to C:\temp\
$user.PictureData |Set-Content "c:\temp\photos\Photo-$($mailbox.windowsemailaddress).jpg" -Encoding byte
}
}
##Catch Error
catch{

##Write photo failure
write-host "No photo for user $($mailbox.windowsemailaddress)"
}
}

Once they are all downloaded, we can upload a photo to a new tenant (or update the existing one) with the below code, you could add a CSV input to map them to the right users if you like too.


##Specify Photo File Path
$FilePath = “<Path to Photo file>”
##Specify User Mailbox
$UserMailbox = “Primary Email of User to Update>"
##Update Photo Without Confirmation
Set-UserPhoto $UserMailbox -PictureData ([System.IO.File]::ReadAllBytes($FilePath)) -confirm $false

This is a nice simple script that can be used in a variety of situations to manipulate user photos in Exchange Online. Hopefully it’s helpful for anyone who needs it!

2 thoughts on “Download users photos from Exchange Online with PowerShell

Leave a comment