The Get-MgUserPhoto cmdlet is a powerful tool for retrieving the profile photos of users in Microsoft 365. This cmdlet can be used to manage or display user photos in applications or reports. In this article, we'll explore various usage examples, including how to get user photo URLs and retrieve user photos from a user list in a CSV file.
Install-Module Microsoft.Graph -Scope CurrentUser.Connect-MgGraph -Scopes "User.Read.All"
Get-MgUserPhoto -UserId "user@domain.com"
Get-MgUserPhoto -UserId "user-id"
To get the URL of a user's photo, you need to format the URL correctly using the user's ID or principal name. Here's an example of how to construct the URL:
$userId = "user@domain.com"
$photoUrl = "https://graph.microsoft.com/v1.0/users/$userId/photo/$value"
Write-Host "User photo URL: $photoUrl"
You can retrieve user photos for multiple users listed in a CSV file. The CSV file should have a column for user IDs or emails.
Example CSV File (users.csv):
UserId
user1@domain.com
user2@domain.com
user3@domain.com
PowerShell Script to Retrieve Photos from CSV
# Import the CSV file
$users = Import-Csv -Path "C:\path\to\your\users.csv"
# Loop through each user and retrieve their photo
foreach ($user in $users) {
$userId = $user.UserId
try {
$photo = Get-MgUserPhoto -UserId $userId
$photoBytes = [System.Convert]::FromBase64String($photo.ContentBytes)
$filePath = "C:\path\to\photos\$($userId).jpg"
[System.IO.File]::WriteAllBytes($filePath, $photoBytes)
Write-Host "Photo saved for user: $userId"
} catch {
Write-Host "Error retrieving photo for user: $userId"
}
}
Yes. Microsoft Graph supports multiple sizes (small, medium, large, and custom dimensions). You can request a specific size using the -Size parameter in the API call, though in PowerShell you’ll typically retrieve the default photo.
If no photo exists, the command will return an error or an empty result. In such cases, you may want to implement error handling or set a default placeholder image in your scripts.
Yes. While Get-MgUserPhoto is for retrieval, you can use Set-MgUserPhotoContent to upload or replace a user’s profile picture. This requires proper permissions and admin rights.
You need User.Read, User.ReadBasic.All, or User.Read.All delegated permissions for standard use. For application-level access, ensure your app is granted the equivalent permissions in Azure AD.
| Error | Cause | Solution |
|---|---|---|
| Access Denied | Insufficient permissions to access user photos. | Ensure you have the necessary permissions. You may need to grant the appropriate permissions in the Azure portal. |
| Invalid UserId | The provided UserId does not correspond to an existing user. | Verify the UserId is correct and corresponds to an existing user in your directory. |
| User Photo Not Found | The user does not have a profile photo set. | Check if the user has uploaded a profile photo. If not, consider handling this scenario in your script, possibly by displaying a default photo. |
The Get-MgUserPhoto cmdlet is versatile and can be tailored to your needs by using various parameters and techniques. By utilizing the cmdlet in combination with PowerShell scripting, you can efficiently retrieve and manage user photos in Microsoft 365.
For more detailed information, you can refer to the official Microsoft documentation.
© m365corner.com. All Rights Reserved. Design by HTML Codex