Entra / Microsoft 365 · Users & guests
Update user photos
A script to illustrate how to update user photos in Microsoft 365 accounts.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-MgGraph -Scopes Directory.ReadWrite.All -NoWelcome
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
Connect-MgGraph -Scopes Directory.ReadWrite.All -NoWelcome# The value of $PhotoLocation should be changed to point to the place where user photos are stored# We expect to find JPG files there named after the first and last names of the UPN assigned to accounts# For example, the photo for Kim.Akers@Office365itpros.com is in Kim.Akers.jpg$PhotoLocation = "c:\UserPhotos\"If (!(Test-Path ($PhotoLocation))) {Write-Host "Can't find $PhotoLocation - please check if this is the right place to find user photos"; break }$i=0# Find Entra ID accounts to updateWrite-Host "Finding user accounts"[array]$Users = Get-MgUser -Filter "assignedLicenses/`$count ne 0 and userType eq 'Member'" `-ConsistencyLevel eventual -CountVariable Records -AllClear-Host$ProgDelta = 100/($Users.Count); $CheckCount = 0; $UserNumber = 0ForEach ($User in $Users) {$UserNumber++$UserStatus = $User.DisplayName + " ["+ $UserNumber +"/" + $Users.Count + "]"Write-Progress -Activity "Checking photo for" -Status $UserStatus -PercentComplete $CheckCount$CheckCount += $ProgDelta# Is EXODS happy with the user photo information for the account?$CheckPhoto = Get-MgUserPhoto -UserId $User.Id -ErrorAction SilentlyContinueIf (!$CheckPhoto) { # No photo found in user account$UserPhoto = $PhotoLocation + $User.UserPrincipalName.Split("@")[0]+".jpg"If (Test-Path $UserPhoto) { # Update the photo because we have a fileWrite-Host "Updating photo for" $User.DisplayName -Foregroundcolor RedSet-MgUserPhotoContent -UserId $User.Id -Infile $UserPhoto$i++; Clear-Host} Else { # No photo file availableWrite-Host "No photo file available for" $User.DisplayName }}}Write-Host "All done. $i User Photos updated"
Attribution
Author
Office365itpros