How can I delete or remote stale devices in Azure AD using Policy or PowerShell ?

EnterpriseArchitect 6,041 Reputation points
2022-08-19T11:45:40.497+00:00

Folks,

How can I delete or remove the old device in Azure AD with ApproximateLastLogonTimeStamp older than 120 days?

Is there any PowerShell or Azure Policy to do that automatically?

Microsoft Security | Microsoft Entra | Microsoft Entra ID
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

Accepted answer
  1. Carlos Solís Salazar 18,196 Reputation points MVP Volunteer Moderator
    2022-08-19T12:05:51.39+00:00

    Hi @EnterpriseArchitect

    Thank you for asking this question on the **Microsoft Q&A Platform. **

    It will be this script in PowerShell:

    $dt = (Get-Date).AddDays(-120)  
    $Devices = Get-AzureADDevice -All:$true | Where {($_.ApproximateLastLogonTimeStamp -le $dt) -and ($_.AccountEnabled -eq $false)}  
    foreach ($Device in $Devices) {  
    Remove-AzureADDevice -ObjectId $Device.ObjectId  
    }  
    

    The following documentation has more details

    Hope this helps,
    Carlos Solís Salazar

    ----------

    Accept Answer and Upvote, if any of the above helped, this thread can help others in the community looking for remediation for similar issues.
    NOTE: To answer you as quickly as possible, please mention me in your reply.


    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.