The smarter way to manage Microsoft 365. Try AdminDroid for free!
Find and Delete Orphaned Users in SharePoint Online

Find and Delete Orphaned Users in SharePoint Online

Summary

SharePoint Online orphaned users are stale user entries that remain in site collections even after their corresponding accounts are deleted from Microsoft Entra ID. To simplify the cleanup process, we’ve developed a PowerShell script that identifies orphaned users and automates cleanup actions, including deletion upon approval.

Ever removed a user from Microsoft 365, only to find their identity still appearing in SharePoint Online? For instance, their name might continue to show in the Created By or Modified By columns, even after their account has been deleted. This is one of the often-overlooked aspects of user offboarding.

Disabling or deleting an account doesn’t automatically remove every trace of that user across SharePoint site collections. These stale user entries, known as orphaned users, can make access management and user cleanup more challenging.

In this blog, we’ll explore what orphaned users are and how to identify them across SharePoint Online site collections. To simplify the cleanup process, we’ve also included a handy PowerShell script that helps you find and remove SPO orphaned users efficiently.

What Are Orphaned Users in SharePoint Online?

SharePoint Online maintains user and permission details for each site collection in a hidden, system-managed User Information List (UIL). This list stores user attributes such as display names, UPNs, login identifiers (typically i:0#.f|membership|[email protected]), and unique identity details like the Microsoft Entra Object ID (AadObjectId).

However, when a user is deleted from Microsoft Entra ID, their corresponding record is not always automatically removed from the SharePoint site’s UIL. These stale entries remain without a matching directory identity and are known as orphaned users.

How to Find Orphaned Users in SharePoint Online

SharePoint Online does not offer a built-in way to identify orphaned users across site collections. Instead, admins should review each site’s People and Groups page by appending /_layouts/15/people.aspx?MembershipGroupId=0 to the site URL and manually cross-check user identities against Microsoft Entra ID.

From there, admins have to select outdated entries and remove them using the Delete Users from the Site Collection option. However, this requires repeating the process site by site, which is tedious and time-consuming.

To simplify orphaned user cleanup, we’ve developed a PowerShell script to identify and remove orphaned users across SharePoint Online site collections after confirmation.

Find and Delete Orphaned Users in SPO Using PowerShell Script

This PowerShell script scans site collections, verifies user entries against Entra ID, identifies potential orphaned users, and removes them directly upon approval.

Download the PowerShell Script: FindAndDeleteOrphanedUsers.ps1

Script Highlights:

  • Exports a list of all orphaned users across your SharePoint Online site collections.
  • Identifies disabled user accounts in the SPO User Information List that may require cleanup to prevent stale entries.
  • Lists orphaned users in a specific SharePoint site collection.
  • Supports the removal of orphaned users upon admin approval.
  • Finds orphaned guest users across SharePoint Online sites.
  • Automatically checks for the PnP PowerShell, and Microsoft Graph modules and installs it with your confirmation if it is missing.
  • Script is scheduler friendly and supports certificate-based authentication.

Sample Output – SharePoint Online Orphaned Users Report

The exported orphaned users report includes details such as Display Name, User Principal Name, Site Name, Site URL, Login Name, Is Site Admin, Permission Levels, SPO Object Id, Entra Status, Matched By, Entra Object Id and User Type.

The generated SPO orphaned users report looks like the screenshot below.

After reviewing the users, delete the rows for those you want to retain. Save and close the file, then select proceed to continue. The remaining users in the report will be considered for removal.

orphaned-users-in-sharepoint-online

Script Execution Methods – Find and Remove Orphaned Users in SharePoint Online Using PowerShell

  1. Download the PowerShell script provided above.
  2. Open PowerShell 7.4 or later and navigate to the folder where the script is saved.
  3. Choose any of the following methods to execute the script.

Note:

This script uses PnP PowerShell and requires a Microsoft Entra ID app registration for authentication. Register an app in Entra ID and then assign the required SharePoint and Microsoft Graph permissions:

Module Name Permission Permission Type
SharePoint Online AllSites.FullControl Delegated (For Interactive)
Microsoft Graph User.Read.All, Directory.Read.All Delegated (For Interactive)
SharePoint Online Sites.FullControl.All Application (For Certificate)
Microsoft Graph User.Read.All, Directory.Read.All Application (For Certificate)

Method 1:

Execute the script interactively for MFA or Non-MFA Accounts.

Upon execution, the script prompts you to authenticate using interactive browser sign-in or certificate-based authentication.

Method 2: Run the script by explicitly providing Entra app credentials.

Provide your tenant name and the Client ID of the registered Microsoft Entra ID app while executing the script. This method prompts interactive sign-in using your browser.

Method 3: Execute the script unattended using certificate-based authentication.

The script prompts the tenant name and Client ID if they are not provided. Ensure that the certificate is associated with the Microsoft Entra ID app registration and your local machine.

You can either create self-signed certificate or use one issued by a Certificate Authority (CA). This method is ideal for automation when scheduling the script through Task Scheduler or Azure Automation for periodic audits. While scheduling, ensure to include -ReportingOnly parameter to bypass user removal.

Points to Remember

  1. Ensure that your Entra application has required redirection URI’s.
    • Mobile and desktop applications – http://localhost
    • Mobile and desktop applications -https://login.microsoftonline.com/common/oauth2/nativeclient
  2. During interactive sign-in, if you’re not an admin or member of certain private sites, you won’t be able to retrieve those site’s user data. These sites will be skipped, and you’ll see a warning like: Attempted to perform an unauthorized operation in error logs.

How to Identify Orphaned Users in SharePoint Sites Using Script

This script includes practical filters for real-world scenarios to help you analyze orphaned user accounts across SharePoint Online sites. Here’s how it improves SharePoint Online user management and governance.

  1. List orphaned users in a specific SharePoint Online site
  2. Identify disabled user accounts across all SPO User Information Lists
  3. Find disabled users in a specific SharePoint Online site
  4. Identify orphaned guest users in SharePoint Online
  5. Find orphaned guest users in a specific SharePoint Online site
  6. Remove orphaned users from SharePoint Online

1. List Orphaned Users in a Specific SharePoint Online Site

By default, the script scans all SharePoint Online site collections to identify potential orphaned users. If you want to investigate a specific site collection, use the -SiteUrl parameter and provide the target site URL.

This is useful when reviewing user access for a particular department or business-critical site without scanning the entire Microsoft 365 tenant for orphaned users.

Replace <SiteURL> with the URL of the SharePoint Online site collection you want to scan. You can also enter multiple site URLs as comma-separated values.

orphaned-users-in-specific-site-collection

2. Identify Disabled User Accounts Across All SPO User Information Lists

Apart from orphaned users, administrators may also need to identify disabled users who remain stale in SharePoint Online. Use the -DisabledUsers parameter to generate a separate report of these users.

This helps administrators review disabled accounts that may still have existing SharePoint permissions or historical references.

disabled-users-in-sharePoint-online

3. Find Disabled Users in a Specific SharePoint Online Site

The above method explains how to identify disabled users across all SharePoint Online site collections. However, if you want to review disabled users in a specific site, use the -SiteUrl parameter and provide the target site collection URL as shown below.

Replace <SiteURL> with the URL of the SharePoint Online site collection you want to scan. You can also enter multiple site URLs as comma-separated values.

Find Disabled Users in Specific SPO Site Collection Using PowerShell

4. Identify Orphaned Guest Users in SharePoint Online

To identify guest users who are orphaned across SharePoint Online site collections, use the -GuestUsers parameter. This generates a report of guest user entries, helping administrators review external users who have access to SharePoint sites.

guest-users-in-sharepoint-online

5. Find Orphaned Guest Users in a Specific SharePoint Online Site

If you want to review guest users in a specific site collection, use the -GuestUsers parameter along with -SiteUrl.

Replace <SiteURL> with the URL of the SharePoint Online site collection you want to scan. You can also enter multiple site URLs as comma-separated values.

guest-users-in-specific-site

6. Remove Orphaned Users from SharePoint Online

To remove confirmed orphaned users without an additional confirmation prompt, use the -ForceRemove parameter. This is useful when automating cleanup across multiple site collections.

You can also use -InputCsv to load a previously reviewed CSV report and remove the selected orphaned users in a later session.

After execution, the script displays the removal results and exports log file containing the Site URL, Display Name, Login Name, Entra Status, Removal Status for further investigation in case of errors.

Remove Orphaned Users From SharePoint Online Using PowerShell Script

Best Practices for Orphaned User Cleanup

Maintaining clean SharePoint identity records requires regular reviews and a consistent cleanup process. Administrators should follow these best practices.

  • Review and cleanup identity mismatches: Compare the SPO user Object ID with the Entra ID Object ID to identify and cleanup stale entries that no longer exist.
  • Review access errors: Investigate site collections that return access-denied errors and ensure the required administrative permissions are available before cleanup.
  • Maintain audit records and perform periodic reviews: Retain reports of identified and removed users for future reference. Schedule regular identity audits and access reviews as part of your Microsoft 365 offboarding practices.

Conclusion

Keeping SharePoint Online free of orphaned user entries helps maintain effective access control and governance. By validating site user information against Microsoft Entra ID and incorporating regular checks into offboarding practices, admins can remove stale identities and keep user records accurate across site collections.

We hope this blog helps you gain better control over identities and permissions in your SharePoint Online environment.

Find and Delete Orphaned Users in SharePoint Online

by Varani Krishna Sai time to read: 6 min
0