Delete Azure AD devices from certain time period

Ridanh 61 Reputation points
2021-11-15T13:00:22.79+00:00

Hi All,

I have used the below script to disable devices in Azure AD from a set period of time, where 90 is the number of days.

$dt = (Get-Date).AddDays(-90)
Get-AzureADDevice -All:$true | Where {$_.ApproximateLastLogonTimeStamp -le $dt} | Set-AzureADDevice -AccountEnabled $false

Would anybody know a similar script to delete devices from a set period of time eg 400 days or would know what I can do to change the above around to use as i need.

thanks

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

Accepted answer
  1. Clément BETACORNE 2,496 Reputation points
    2021-11-15T13:30:41.727+00:00

    Hello Ridanh,

    You can use something like that :

    $dt = (Get-Date).AddDays(-400)
    Get-AzureADDevice -All:$true | Where {$_.ApproximateLastLogonTimeStamp -le $dt} | Remove-AzureADDevice
    

    Regards,

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Ridanh 61 Reputation points
    2021-11-16T12:26:11.283+00:00

    Hi, i have tried the below and have error returned

    PS C:\WINDOWS\system32> $dt = (Get-Date).AddDays(-1400)
    PS C:\WINDOWS\system32> $dt = (Get-Date).AddDays(-1400) Get-AzureADDevice -All:$true | Where {$_.ApproximateLastLogonTimeStamp -le $dt} | Remove-AzureADDevice
    At line:1 char:33

    • $dt = (Get-Date).AddDays(-1400) Get-AzureADDevice -All:$true | Where ...
    • ~~~~~~~~~~~~~~~~~
      Unexpected token 'Get-AzureADDevice' in expression or statement.
    • CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    • FullyQualifiedErrorId : UnexpectedToken

  2. Ridanh 61 Reputation points
    2021-11-16T13:03:52.94+00:00

    Thank you that worked.

    0 comments No comments

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.