Friday, 25 July 2025

Microsoft Intune:Automating the registration of multiple devices for Windows Autopilot using PowerShell

Automating the registration of multiple devices for Windows Autopilot using PowerShell is a common and efficient approach. This typically involves collecting the hardware hash from each device and then uploading it to Microsoft Intune (via Microsoft Graph).

 

SYNOPSIS Automates the registration of multiple Windows devices to Autopilot. Reads computer names from a CSV, collects hardware hashes remotely, and uploads them to Microsoft Intune/Autopilot.

 

DESCRIPTION: This script facilitates bulk Autopilot device registration. It iterates through a list of computer names provided in a CSV file, attempts to connect to each device, retrieves its hardware hash using the Get-WindowsAutopilotInfo PowerShell module, and then uploads this information directly to the Autopilot service via Microsoft Graph. Concurrency is managed through PowerShell jobs to process multiple devices simultaneously. 


Save the hardware hash locally on a device as a CSV file

https://learn.microsoft.com/en-us/autopilot/add-devices  


# === CONFIGURATION ===

$csvPath = "C:\scripts\computernamelist.csv"

$maxConcurrentJobs = 10

$jobs = @()


# === ENVIRONMENT SETUP ===

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned -Force

Install-Script -Name Get-WindowsAutopilotInfo -Force -ErrorAction Stop


# Authenticate to Microsoft Graph

Connect-MgGraph -Scopes "DeviceManagementServiceConfig.ReadWrite.All"


# === FUNCTION: Remove Computer from CSV ===

function Remove-ComputerFromCSV {

    param (

        [string]$computerName,

        [string]$csvPath

    )

    $updatedList = (Import-Csv -Path $csvPath) | Where-Object { $_.ComputerName -ne $computerName }

    $updatedList | Export-Csv -Path $csvPath -NoTypeInformation

    Write-Host "[INFO] Removed '$computerName' from CSV after successful import."

}


# === MAIN PROCESS ===

$computers = Import-Csv -Path $csvPath


foreach ($computer in $computers) {

    $computerName = $computer.ComputerName


    $job = Start-Job -ScriptBlock {

        param($computerName, $csvPath)


        try {

            if (Test-Connection -ComputerName $computerName -Count 1 -Quiet) {

                Write-Host "[INFO] $computerName is online. Starting Autopilot import..."


                [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

                Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned -Force


                $result = Get-WindowsAutopilotInfo -Online -Name $computerName


                if ($result -like "*devices imported successfully*") {

                    Write-Output "[SUCCESS] $computerName - Imported successfully."

                    Remove-ComputerFromCSV -computerName $computerName -csvPath $csvPath

                } elseif ($result -like "*error 806 ZtdDeviceAlreadyAssigned*") {

                    Write-Output "[WARNING] $computerName - Device already assigned."

                } else {

                    Write-Output "[ERROR] $computerName - Unknown issue occurred."

                }

            } else {

                Write-Output "[OFFLINE] $computerName is not reachable."

            }

        } catch {

            Write-Output "[EXCEPTION] $computerName - $_"

        }


    } -ArgumentList $computerName, $csvPath


    $jobs += $job

    Write-Host "[INFO] Started job for $computerName (Job ID: $($job.Id))"


    # Manage concurrent job limits

    if ($jobs.Count -ge $maxConcurrentJobs) {

        $jobs | ForEach-Object {

            $_ | Wait-Job

            $output = Receive-Job -Job $_

            Write-Host $output

            Remove-Job $_

        }

        $jobs = @()

    }

}


# === FINAL JOB CLEANUP ===

$jobs | ForEach-Object {

    $_ | Wait-Job

    $output = Receive-Job -Job $_

    Write-Host $output

    Remove-Job $_

}



PowerShell Script for Bulk Autopilot Device Registration

This script will:

  1. Read a list of computer names from a CSV file.
  2. Ping each computer to check if it's online.
  3. Remotely execute the Get-WindowsAutopilotInfo command to collect the hardware hash.
  4. Upload the hardware hash to Microsoft Autopilot (Intune) directly.
  5. Provide feedback on success or failure for each device.
  6. (Optional but recommended) Remove successfully processed devices from the CSV to track progress.