I want delete users that have a blocked sign in and users who have not signed in over 90 days

Robert Rajnys 0 Reputation points
2024-05-07T15:23:55.68+00:00

I need help deleting users by last sign in date and have a blocked sign in

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,865 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Harpreet Singh Matharoo 7,576 Reputation points Microsoft Employee
    2024-05-08T10:58:57.79+00:00

    Hello @Robert Rajnys ,

    Thank you for reaching out to Microsoft QnA platform. I would like to confirm that you can export list of all users with Account Enabled Status and LastSignInDateTime.

    You can use following command from Microsoft Graph PowerShell Beta module.

    Get-MgBetaUser -All -Property 'AccountEnabled','UserPrincipalName','SignInActivity','Mail','DisplayName' | Select-Object @{N='UserPrincipalName';E={$_.UserPrincipalName}}, @{N='AccountEnabled';E={$_.AccountEnabled}}, @{N='DisplayName';E={$_.DisplayName }}, @{N='LastSignInDate';E={$_.SignInActivity.LastSignInDateTime}} | Export-Csv -Path C:\usernew1.csv -NoTypeInformation -NoClobber
    
    

    From this list you should be able to figure out all the blocked/disabled users and delete them later.

    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.

    0 comments No comments

  2. Navya 4,780 Reputation points Microsoft Vendor
    2024-05-09T12:32:14.75+00:00

    Hi @Robert Rajnys

    Thank you for posting this in Microsoft Q&A.I understand that you want delete users that have a blocked sign in and users who have not signed in over 90 days.

    Below is the sample PowerShell script

    Connect-MgGraph -Scopes 'AuditLog.Read.All'
    $90Days = (Get-Date).AddDays(-90)
    Get-MgUser -All -Property 'UserPrincipalName','SignInActivity','AccountEnabled' | Select-Object @{N='UserPrincipalName';E={$_.UserPrincipalName}}, @{N='LastSignInDate';E={$_.SignInActivity.LastSignInDateTime}}, @{N='AccountEnabled';E={$_.AccountEnabled}}| Where-Object { $_.LastSignInDate -lt $90Days -and $_.AccountEnabled -ne 'True'}
    
    
    

    Hope this helps. Do let us know if you any further queries.

    Thanks,

    Navya.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it.