-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRemove-WindowsAutopilotDeviceBySerialNumber.ps1
More file actions
66 lines (51 loc) · 2.29 KB
/
Remove-WindowsAutopilotDeviceBySerialNumber.ps1
File metadata and controls
66 lines (51 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
###################################################################################################################
# Name: Remove-WindowsAutopilotDeviceBySerialNumber.ps1
# Author: Thomas Marcussen, Thomas@ThomasMarcussen.com
# Date: January, 2026
###################################################################################################################
# ==============================
# PROMPT FOR SERIAL NUMBER
# ==============================
$SerialNumber = Read-Host "Enter the serial number of the Autopilot device to delete"
if ([string]::IsNullOrWhiteSpace($SerialNumber)) {
Write-Error "No serial number entered. Exiting."
return
}
# Normalize input (helps with copy/paste issues)
$SerialNumber = $SerialNumber.Trim().ToUpper()
# ==============================
# CONNECT TO GRAPH
# ==============================
Import-Module Microsoft.Graph.DeviceManagement
Connect-MgGraph -Scopes "DeviceManagementServiceConfig.ReadWrite.All"
# ==============================
# FIND AUTOPILOT DEVICE
# ==============================
Write-Host "Searching for Autopilot device with serial number: $SerialNumber" -ForegroundColor Cyan
$Devices = Get-MgDeviceManagementWindowsAutopilotDeviceIdentity `
-Filter "contains(serialNumber,'$SerialNumber')"
if (-not $Devices) {
Write-Warning "No Autopilot device found with serial number: $SerialNumber"
Disconnect-MgGraph
return
}
# ==============================
# CONFIRM & DELETE
# ==============================
foreach ($Device in $Devices) {
Write-Host ""
Write-Host "Autopilot device found:" -ForegroundColor Yellow
Write-Host " Serial Number : $($Device.SerialNumber)"
Write-Host " Model : $($Device.Model)"
Write-Host " Manufacturer : $($Device.Manufacturer)"
Write-Host " Device ID : $($Device.Id)"
$Confirm = Read-Host "Type DELETE to confirm removal of this Autopilot device"
if ($Confirm -ne "DELETE") {
Write-Warning "Deletion cancelled for serial number $($Device.SerialNumber)"
continue
}
Remove-MgDeviceManagementWindowsAutopilotDeviceIdentity `
-WindowsAutopilotDeviceIdentityId $Device.Id
Write-Host "✅ Autopilot device deleted successfully" -ForegroundColor Green
}
Disconnect-MgGraph