Script Repository


Set user photo from Microsoft 365 to AD account

February 18, 2021
857

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
try
{
    $objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
}
catch
{
    return
}

try
{
    # Connect to Exchange Online
    $session = $Context.CloudServices.CreateExchangeOnlinePSSession()
    Import-PSSession $session -AllowClobber -DisableNameChecking -CommandName "Get-UserPhoto" 
    
    # Get user photo
    $picture = Get-UserPhoto $objectId.ToString() -ErrorAction SilentlyContinue
}
finally
{
    # Close the remote session and release resources
    if ($session) { Remove-PSSession $session }
}

if ($NULL -eq $picture.PictureData)
{
    return
}

# Update user photo in AD
$user = $Context.BindToObjectEx($Context.TargetObject.AdsPath, $True)
$user.Put("thumbnailPhoto", $picture.PictureData)
$user.SetInfo()

Comments ( 2 )
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?
Leave a comment