Another fun day at work. Request to get all the pictures of employees/consultants who have set a profile picture in Teams/Outlook/M365. After a little google search and experimentation, it IS actually NOT that difficult.
The process in a few steps :
- Connect to ExchangeOnline
- Get a list of all active users in ExchangeOnline
- Loop through the list of users
- Check if this user has a picture set, and if so, download the picture
The actual script (you can copy/paste everything)
### Connection to Exchange Online
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline # Connect with your admin account on 365
### Input Parameters
$folderpath="C:\temp\USERPICTURES\"
### Download all user profile pictures from Office 365
# Create folder if not exists
New-Item -ItemType directory -Path $folderpath –force
# Get all users from Exchange Online
Write-Host "Getting all users from 365. Please wait."
$allUsers=Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited|select UserPrincipalName,Alias
Write-Host "Looping through users, and start picture download."
Foreach($user in $allUsers)
{
# Set output filename
$path=$folderpath+$user.UserPrincipalName+".Jpg"
# Check if user has a picture
$haspicture = Get-EXOMailbox -identity $user.UserPrincipalName -Properties HasPicture
# Set True or False var zo we can ignore the ones without picture
$haspicture2 = $haspicture.HasPicture
if($haspicture2) { # If true, get the picture
$photo=Get-Userphoto -identity $user.UserPrincipalName
If($photo.PictureData -ne $null)
{
[io.file]::WriteAllBytes($path,$photo.PictureData)
Write-Host $user.Alias “profile picture downloaded”
}
} else {
Write-Host $user.Alias "has no picture."
}
}
Change this variable to change the output folder path:
$folderpath=”C:\temp\USERPICTURES\”