We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script Repository

Set user photo from Microsoft 365 to AD account

February 12, 2024 Views: 1584

The script gets a user photo from Microsoft 365 and sets it into the AD account of the user. To run the script, create a custom command or scheduled task configured for the User object type.

Edit Remove
PowerShell
# Get user ID in Microsoft 365
$azureID = $Context.TargetObject.AzureID
if ($NULL -eq $azureID)
{
    $Context.LogMessage("The user does not have a Microsoft 365 account", "Warning")
    return
}

# Connect to Microsoft Graph PowerShell
$accessToken = $Context.CloudServices.GetAzureAuthAccessToken()
$accessToken | out-file c:\token.txt
Connect-MgGraph -AccessToken ($accessToken | ConvertTo-SecureString -AsPlainText -Force)

# Get temp file path
$tempFodlerPath = [System.IO.path]::GetTempPath()
$tempFilePath = "$tempFodlerPath\photo.tmp"

# Download user photo
try
{
    Get-MgUserPhotoContent -UserId $azureID -OutFile $tempFilePath
}
catch
{
    $Context.LogMessage("The user has no photo", "Warning")
}

$photoBytes = [System.IO.File]::ReadAllBytes($tempFilePath)
if ($NULL -ne $photoBytes)
{
    # Update user photo in AD
    $user = $Context.BindToObjectEx($Context.TargetObject.AdsPath, $True)
    $user.Put("thumbnailPhoto", $photoBytes)
    $user.SetInfo()
}

# Remove temp file
Remove-Item $tempFilePath -Force -Confirm:$False
Comments 6
avatar
A Aug 31, 2021
I have this setup but is there a way to do a bulk import from 365 for all users
avatar
Support Sep 01, 2021
Hello,

What exactly do you mean? Could you, please, describe the desired behavior in all the possible details with live examples?
avatar
Trent Jul 11, 2023
We would like to do this for all users, is that possible?
avatar
Support Jul 12, 2023
Hello Trent,

As it is mentioned in the script description, it can be executed in a scheduled task or custom command. As such, the easiest option is to create a one-time task executing the script and add All objects to the Activity Scope.
avatar
Kevin Apr 08, 2024
Hi,

Microsoft annouched they will stop the get-userphoto cmdlet from Exchange Online. Will you be upgrading the script to the Microsoft Graph PowerShell ((Updated) ExchangePowerShell: retirement of tenant admin cmdlets to Get, Set, and Remove UserPhotos)?
avatar
Support Apr 08, 2024
Hello Kevin,

The script uses the Get-MgUserPhotoContent cmdlet from the Microsoft.Graph PowerShell module to get the photo. Please, note that for the script to work, the module must be installed on the computer where the Adaxes service runs. If you have multiple Adaxes services sharing a common configuration, the module must be installed on each computer where the Adaxes service runs.
avatar
Kevin Apr 10, 2024
Awesome then we don't have to fix anything for the upcomming update of the powershell cmdlets for Exchange.
Leave a comment
Loading...

Got questions?

Support Questions & Answers