User Photo last modified date

Vijay Ukalkar 0 Reputation points
2024-03-08T02:18:07.0566667+00:00

We use an outsourced HR system, and we're looking to sync our organization's Microsoft 365 (M365) profile photos with the respective user profiles in our HR system. To do this, we've developed a script to download all the profile photos. However, we need to modify the script to include the last modified date. Specifically, we want to download only those profile photos that the user have been updated within the last 24 hours on their M365 accounts. This way, we can avoid unnecessary downloads and uploads to our vendor's HR FTP server.

While researching I found that we cannot find about the user's last modified photo from the Azure AD or from the PowerShell. I am trying to find out any way where I can download all the last 24 updated user profile photo., maybe from onprem AD or SharePoint.

By the way below is my script.

Import required modules

Import-Module Az.Storage

Import-Module ExchangeOnlineManagement

Connect-MicrosoftGraph

Define your Azure Blob Storage account details

$storageAccountName = "photoaccount"

$storageAccountKey = "mystoragekey"

$containerName = "photos"

Connect to the Azure Storage Account

$storageContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

Get all users from Microsoft 365

$users = Get-MgUser -All

Iterate through each user and download their photo

foreach ($user in $users) {

$userId = $user.Id

$photoFileName = "$($user.UserPrincipalName).jpg"  # Adjust the file extension if necessary

$tempPhotoFile = Join-Path -Path $env:TEMP -ChildPath $photoFileName



# Download the user's photo to a temporary file

Get-MgUserPhotoContent -UserId $userId -OutFile $tempPhotoFile -ErrorAction SilentlyContinue



if (Test-Path $tempPhotoFile) {

    # Upload the photo to Azure Blob Storage

    Write-Host "Uploading photo for $($user.UserPrincipalName)..."

    Set-AzStorageBlobContent -Container $containerName -Blob $photoFileName -File $tempPhotoFile -Context $storageContext -Force

    Write-Host "Photo for $($user.UserPrincipalName) uploaded successfully."


    

    # Clean up the temporary file

    Remove-Item $tempPhotoFile -Force

} else {

    Write-Host "No photo found for $($user.UserPrincipalName)."

}
```}

Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
4,363 questions
Microsoft Exchange Online
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,300 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,810 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. James Hamil 23,216 Reputation points Microsoft Employee
    2024-03-08T21:13:54.6333333+00:00

    Hi @Vijay Ukalkar , unfortunately, as you mentioned, there is no direct way to get the last modified date of a user's profile photo from Azure AD or PowerShell. However, there might be a workaround that you can try.

    One possible solution is to use SharePoint Online to store the user's profile photos instead of Azure Blob Storage. SharePoint Online stores the user's profile photo as a file, and you can use SharePoint Online PowerShell cmdlets to get the last modified date of the file. Here's an example of how you can modify your script to use SharePoint Online:

    Import the SharePoint Online PowerShell module:

    Import-Module Microsoft.Online.SharePoint.PowerShell
    

    Connect to SharePoint Online:

    Connect-SPOService -Url https://yourtenant-admin.sharepoint.com
    

    Get the user's profile photo from SharePoint Online:

    $photoUrl = "https://yourtenant-my.sharepoint.com/User%20Photos/Profile%20Pictures/$($user.UserPrincipalName)_MThumb.jpg"
    $photoFile = Join-Path -Path $env:TEMP -ChildPath $photoFileName
    Invoke-WebRequest -Uri $photoUrl -OutFile $photoFile
    

    Get the last modified date of the file:

    $photoItem = Get-SPOFile -Url "/User Photos/Profile Pictures/$($user.UserPrincipalName)_MThumb.jpg"
    $lastModifiedDate = $photoItem.TimeLastModified
    

    Check if the file has been modified within the last 24 hours:

    $lastModifiedDateTime = [DateTime]::Parse($lastModifiedDate)
    $timeSpan = New-TimeSpan -Start $lastModifiedDateTime -End (Get-Date)
    if ($timeSpan.TotalHours -lt 24) {
        # Upload the photo to Azure Blob Storage
        Write-Host "Uploading photo for $($user.UserPrincipalName)..."
        Set-AzStorageBlobContent -Container $containerName -Blob $photoFileName -File $photoFile -Context $storageContext -Force
        Write-Host "Photo for $($user.UserPrincipalName) uploaded successfully."
    } else {
        Write-Host "No photo found for $($user.UserPrincipalName) or the photo was not modified within the last 24 hours."
    }
    

    Please let me know if you have any questions and I can help you further.

    If this answer helps you please mark "Accept Answer" so other users can reference it.

    Thank you,

    James

    1 person found this answer helpful.