Back to script library
Entra / Microsoft 365 · Groups

Update group photos sdk

Example script to show how to check the photos for Microsoft 365 Groups against a list of approved photos.

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.

[array]$LabelsToCheck = "d6cfd185-f31c-4508-ae40-229ff18a9919", "c99e52c6-f5ff-4050-9313-ca6a3a35710f"
$PhotoDirectory = "c:\temp\GroupPhotos\"
Connect-MgGraph -Scopes Directory.ReadWrite.All -NoWelcome
[int]$i=0
[array]$Groups = Get-MgGroup -Filter "groupTypes/any(c:c eq 'unified')" -All
# Filter groups to extract those with the desired labels
$Groups = $Groups | Where-Object {$_.AssignedLabels.LabelId -in $LabelsToCheck} | Sort-Object DisplayName
Write-Host ("Scanning {0} groups to check photos..." -f $Groups.count)
# Read in data about approved photos
[array]$GroupPhotos = Import-csv c:\temp\GroupPhotos.csv
ForEach ($Group in $Groups) {
$ExistingPhotoData = $Null
# Do we have some photo data?
$Photo = $GroupPhotos | Where-Object {$_.Id -eq $Group.Id}
If ($Photo) { # We do!
$PhotoFile = ("{0}{1}.jpg" -f $PhotoDirectory, $Photo.Id)
# Check if a photo file is where we expect it to be
If ((Test-Path $PhotoFile) -eq $False) {
Write-Host ("Group {0} requires an approved photo but the expected file is not available in {1}" -f $Group.DisplayName, $PhotoFile)
$PhotoFile = $Null
}
Write-Host ("Checking photo for group {0}" -f $Group.DisplayName)
$ExistingPhotoData = Get-MgGroupPhoto -GroupId $Group.Id -ErrorAction SilentlyContinue
If ($ExistingPhotoData) {
Write-Host ("Group {0} has a photo" -f $Group.DisplayName)
} Else {
Set-MgGroupPhotoContent -GroupId $Group.Id -InFile $PhotoFile
$i++
$Photo | Add-Member -NotePropertyName Photo -NotePropertyValue $PhotoFile -Force
}
} Else { # No photo available
Write-Host ("Group {0} requires an approved photo but no entry is available in the photo list" -f $Group.DisplayName)
}
} # End Foreach group
Write-Host ("All done. {0} photos updated." -f $i)
Attribution