Functions/Remove-IntuneAutopilotDevice.ps1
function Remove-IntuneAutopilotDevice { # version 1 [CmdletBinding()] param ( [Parameter()][string]$DeviceName, [Parameter()][string]$Serial, [Parameter()][switch]$Confirm ) iF (!($DeviceName) -and !($Serial)) { break } if ($DeviceName) { $IntuneDevice = Get-DeviceManagement_ManagedDevices -Verbose | Get-MSGraphAllPages | Where-Object deviceName -EQ $DeviceName | Select-Object * } if ($Serial) { $IntuneDevice = Get-DeviceManagement_ManagedDevices -Verbose | Get-MSGraphAllPages | Where-Object deviceName -EQ $Serial | Select-Object * } #$URI = "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeviceIdentities?`$filter=contains(serialNumber,'$($IntuneDevice.serialNumber)')" #$URI #$AutopilotDevice = Invoke-MSGraphRequest -Url $URI -HttpMethod GET -ErrorAction Ignore -Verbose if ($IntuneDevice) { $AutopilotDevice = Get-AutopilotDevice -serial $IntuneDevice.serialNumber } if ($Serial) { $AutopilotDevice = Get-AutopilotDevice -serial $Serial } #$AADDevice = Get-AzureADDevice -SearchString $DeviceName if ($IntuneDevice) { $AADDevice = Get-AzureADDevice -All $true | Where-Object deviceId -EQ $IntuneDevice.azureADDeviceId } if ($AutopilotDevice) { $AADDevice = Get-AzureADDevice -All $true | Where-Object deviceId -EQ $AutopilotDevice.azureAdDeviceId } "Intune device details:" $IntuneDevice | Format-Table "Autopilot device details:" $AutopilotDevice | Format-List "AzureAD device details:" $AADDevice | Format-Table if ($Confirm) { if ($IntuneDevice) { Remove-DeviceManagement_ManagedDevices -managedDeviceId $IntuneDevice.id -Verbose } if ($AutopilotDevice) { #$URI = "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeviceIdentities/$($AutopilotDevice.value.Id)" #$URI #Invoke-MSGraphRequest -Url $URI -HttpMethod DELETE #Get-AutopilotDevice -serial $IntuneDevice.serialNumber | Remove-AutopilotDevice -Verbose Get-AutopilotDevice -id $AutopilotDevice.id | Remove-AutopilotDevice Write-Host "Get-AutopilotDevice -id $($AutopilotDevice.id) | Remove-AutopilotDevice" } if ($AADDevice) { Remove-AzureADDevice -ObjectId $AADDevice.ObjectId -Verbose } $IntuneDevice.serialNumber } } #Remove-IntuneAutopilotDevice -DeviceName "Device01" |