↓Skip to main content

Remove User Photos in Microsoft Entra ID in Bulk with Microsoft Graph

·174 words·1 min
Table of Contents

This blog post will guide you through a PowerShell script that uses the Microsoft Graph PowerShell module to remove user photos in bulk.

This can be particularly useful for cleaning up disabled user accounts.

Prerequisites #

Before you begin, ensure you have the following:

  • Microsoft Graph PowerShell Module: Install it by running Install-Module Microsoft.Graph -Force in your PowerShell terminal.
  • Permissions: Ensure you have the necessary permissions to read user profiles and modify user photos.
# Import the Microsoft Graph module
Import-Module Microsoft.Graph

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.Read.All","ProfilePhoto.ReadWrite.All"

# Get all disabled user accounts and output to console
$disabledUsers = Get-MgUser -Filter "accountEnabled eq false" -All

# Output List of disabled Users
$disabledUsers | Format-Table DisplayName, UserPrincipalName, AccountEnabled

# Remove user photo for each disabled user
foreach ($user in $disabledUsers) {
    try {
        Remove-MgUserPhoto -UserId $user.Id -Confirm:$false
        Write-Output "Removed photo for user: $($user.UserPrincipalName)"
    } catch {
        Write-Output "Failed to remove photo for user: $($user.UserPrincipalName). Error: $_"
    }
}

# Disconnect from Microsoft Graph
Disconnect-MgGraph
More Info at Microsoft Learn

Hope that helps!

SamilyIT
Author
SamilyIT
Maybe someone forget to set the author for this post? Or maybe it was intentional?
In reality, this post is a product of our combined efforts.