How to Find All Inactive Guests in Microsoft 365

Guest users are great for enabling collaboration in Microsoft 365, but they can quickly get out of control when left unmanaged. Most organisations I work with have inactive guest accounts cluttering their environment, each representing a security risk which could be avoided.

The problem is simple, guest users accumulate over time through partnerships and collaborations with external parties, but in most cases, no one thinks to remove their access once the work is done. This just creates a growing collection of dormant access to resources, even when their business justification for accessing data may have expired a long time ago.

In this article, I am going to show you how to identify inactive guest users in your environment as well as how to remove them.

Rationale behind removing inactive guest users

Inactive guest accounts represent a strong attack vector into your environment. If persistent access to your environment is maintained for longer than required, attackers may look at utilising this access if they compromise the home organisation of the guest. The guest access could then be used to exploit various techniques of moving laterally within the tenant, escalating their privilege and cementing their access through creating access persistence.

From a compliance perspective, by not removing these inactive guests you are maintaining users’ access to resources for longer than required. ISO27001 states that a person’s access rights should be immediately revoked when they no longer require access to information assets, which could negatively impact compliance, or as a knock-on effect, insurance premiums in the future.

Identifying inactive guest users in Microsoft Entra

If you are looking at tidying up your guest accounts, the first step is enabling visibility into which accounts are a candidate for being deleted. You can use the ID Governance blade if you are licensed for ID Governance to discover quite quickly which guest users are inactive. Follow these steps:

  1. Log in to https://entra.microsoft.com/
  2. Select ID Governance > Dashboard
  3. Click View inactive guests

Below is an example from this page. As you can see, a full list of inactive users can be downloaded, while you also have the ability to filter users based on the activity period. In my opinion, you should be identifying users which have not logged in for 30 days as a maximum.

Inactive Guest users from Entra portal
Inactive Guest users from Entra portal

Identifying inactive guest users with Microsoft Graph

For a more programmatic approach, ensuring you can repeatedly report on this data automatically, you can use Microsoft Graph PowerShell to obtain the same results.

In the below example, you can see all inactive guest users who have not successfully signed in in the last 30 days.

Connect-MgGraph -scopes User.Read.All, AuditLog.Read.All
$Date = (Get-Date).AddDays(-30).ToString("yyyy-MM-ddTHH:mm:ssZ")
$Inactiveusers = Get-MgBetaUser -All -Filter "signInActivity/lastSuccessfulSignInDateTime le $Date" -Property Id, displayName, userPrincipalName, signInactivity, userType
$InactiveGuests = $Inactiveusers | Where-Object { $_.UserType -eq 'Guest' }

You may notice the inefficiencies in the above example, this is because you cannot combine these multiple filters into the same filter string.

In another approach, I wanted to use the ID Governance Graph APIs to obtain this data. Upon inspecting the requests the web browser makes to the Graph API when viewing the inactive guest accounts, I noticed the “beta/identitygovernance/insights/guestInactivityInsights” endpoint.

After consenting to the Insights-GuestActivity.Read.All permission and querying the endpoint, I am only greeted with the following error:

Invoke-MgGraphRequest: GET https://graph.microsoft.com/beta/identitygovernance/insights/guestInactivityInsights?$top=100&$expand=user
HTTP/2.0 401 Unauthorized
Vary: Accept-Encoding
Strict-Transport-Security: max-age=31536000
request-id: 37ea50a9-a09d-4fbb-8fae-d85d47001908
client-request-id: 528b4d95-9661-423f-a853-17459ec24c5a
x-ms-ags-diagnostic: {“ServerInfo”:{“DataCenter”:”UK South”,”Slice”:”E”,”Ring”:”5″,”ScaleUnit”:”006″,”RoleInstance”:”LN2PEPF00015FFC”}}
Link: <https://developer.microsoft-tst.com/en-us/graph/changes?$filterby=beta,PrivatePreview:groupPeerOutlierInsights&from=2022-02-01&to=2022-03-01>;rel=”deprecation”;type=”text/html”, <https://developer.microsoft-tst.com/en-us/graph/changes?$filterby=beta,PrivatePreview:groupPeerOutlierInsights&from=2022-02-01&to=2022-03-01>;rel=”deprecation”;type=”text/html”, <https://developer.microsoft-tst.com/en-us/graph/changes?$filterby=beta,PrivatePreview:guestInactivityInsights&from=2022-10-01&to=2022-11-01>;rel=”deprecation”;type=”text/html”, <https://developer.microsoft-tst.com/en-us/graph/changes?$filterby=beta,PrivatePreview:guestInactivityInsights&from=2022-10-01&to=2022-11-01>;rel=”deprecation”;type=”text/html”
deprecation: Thu, 21 Oct 2021 23:59:59 GMT
sunset: Sat, 21 Oct 2023 23:59:59 GMT
Date: Fri, 18 Jul 2025 05:20:50 GMT
Content-Type: application/json

I am unsure if this represents the endpoint being in private preview or whether it is being deprecated…

Removing inactive guest users

Removing guest users is simple. From the Entra admin center, simply navigate to the users blade and delete the guest users identified as inactive. 

If you want to programmatically delete these users, a loop is required. For this, you must connect to Microsoft Graph with the User.ReadWrite.All permission.

foreach ($Guest in $InactiveGuests) {
    try {
        Write-Host "Deleting user: $($Guest.userPrincipalName)" -ForegroundColor Yellow
        Remove-MgBetaUser -UserId $Guest.Id
        Write-Host "Successfully deleted user: $($Guest.userPrincipalName)" -ForegroundColor Green
    }
    catch {
        Write-Host "Failed to delete user: $($Guest.displayName) - Error: $($_.Exception.Message)" -ForegroundColor Red
    }
} 

Daniel Bradley

Daniel Bradley is a UK-based technical architect and Microsoft MVP in Security. He specialises in Microsoft Entra, Intune, and Microsoft Graph PowerShell and is the author of Microsoft Graph PowerShell for Administrators. He writes and edits Our Cloud Network using experience gained from working with Microsoft cloud environments.

This Post Has 2 Comments

  1. John

    I spent a lot of time looking at User signInActivity while building a guest user clean up process. While spot checking some guests identified for deletion I noticed some things that didn’t add up.

    Long story short, I wrote something that compared user object signInActivity to SigninLogs and AADNonInteractiveUserSignInLogs. This found a large number data problems where signInActivity did not accurately reflect the true signin activity. I opened a case with MSFT. There was back and forth for 4 months. Them telling me it was “fixed”, me running my script and telling them it isn’t. I finally gave up and we’ve built our own signInActivity persistence for a more accurate guest user clean up.

Leave a Reply