Search code examples
windowsazurepowershellcsvimport-csv

Bulk administration using csv import to remove device from Azure AD list


I am trying to create a PowerShell script to cleanup Azure AD devices. I figured I should probably find a combination of these:

Get-MsolDevice -all | select-object -Property Enabled, DeviceId, DisplayName, DeviceTrustType, ApproximateLastLogonTimestamp | export-csv C:\Temp\devicelist-summary.csv

and

Import-Csv 'C:\Temp\devicelist-summary.csv' | ForEach-Object {}

and

Remove-MsolDevice -DeviceId “device_ID_number” -Force

Then ultimately depending on ApproximateLastLogonTimestamp I would remove them from the Azure AD device list. Unfortunately I have few knowledge in coding so I am kind of stuck, I tried my best but it would be very helpful is someone could help me. I think I am close to something here. Thank you!!


Solution

  • Are you logged on and do you receive the devices when running Get-MsolDevice?

    If you do, you might want to do something like:

    Get-MsolDevice -all | Where-Object {$_.ApproximateLastLogonTimestamp -gt $(Get-Date).AddDays(-180)} | Foreach-Object {Remove-MsolDevice -DeviceId $_.DeviceId -Force}
    

    This saves you from creating a CSV file.

    You might want to split the one-liner up in a few steps and then add a log on all the devices you delete.