Add or Update User Photo
The script streamlines the process of updating user photos of images stored locally. It ensures efficient handling of both successful updates and errors, providing an organized approach to managing user photos within Microsoft 365
Summary
The script streamlines the process of updating user photos of images stored locally. It ensures efficient handling of both successful updates and errors, providing an organized approach to managing user photos within Microsoft 365.

Limitations
Maximum FileSize is 4MB (REST request limit is 4MB)
Config parameter:
$imageSourcePath
$upnDomain
name of the image file has to be the username without the domain
| Name | Filename | UPN |
|---|---|---|
| John Doe | j.doe.jpg | j.doe@contoso.com |
$upnDomain="@contoso.com"
$imageSourcePath="c:\temp\images\"
$completeFolder="Done"
$errorFolder="Error"
Connect-MgGraph -Scopes "User.ReadWrite"
if(-not(Test-Path $imageSourcePath)){
Write-Host "Image Source Path not exists, Please Check the ConfigValue imageSourcePath"
exit(2)
}
$imageFiles=Get-ChildItem -Path $imageSourcePath -Filter *.jpg
Import-Module Microsoft.Graph.Users
foreach($imageFile in $imageFiles){
$username=$imageFile.Name.Substring(0,$imageFile.Name.lastIndexof('.'));
$content=Get-Content $imageFile.VersionInfo.FileName;
$upn ="$username$upnDomain"
try{
$Error.Clear()
Set-MgUserPhotoContent -UserId $upn -InFile $imageFile.VersionInfo.FileName
if($Error.Count -gt 0){
throw "Error in Set-MgUserPhotoContent"
}
$completePath = Join-Path -Path $imageSourcePath -ChildPath $completeFolder
if(-not(Test-Path $completePath)){
New-Item -ItemType Directory -Path $completePath
}
Move-Item -Path $imageFile.VersionInfo.FileName -Destination $completePath -Force
}catch{
# Write-Host $error
$errorPath = Join-Path -Path $imageSourcePath -ChildPath $errorFolder
if(-not(Test-Path $errorPath)){
New-Item -ItemType Directory -Path $errorPath
}
$Error|Set-Content -Path "$errorPath\$username.error.txt"
Move-Item -Path $imageFile.VersionInfo.FileName -Destination $errorPath -Force
}
}
Contributors
| Author(s) |
|---|
| Peter Paul Kirschner |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
Tool documentation & references
- Want to learn more about PnP PowerShell and the cmdlets
Check out the PnP PowerShell site to get started and for the reference to the cmdlets.
- Want to learn more about Microsoft Graph PowerShell SDK and the cmdlets
Check out the Microsoft Graph PowerShell SDK documentation site to get started and for the reference to the cmdlets.