Bulk Autopilot device renaming

Niklas Tinner
2 min readSep 12, 2023

--

This post is just a little documentation on Autopilot device renaming with PowerShell.

Autopilot attributes

Find more information on Autopilot identities and assignments (oceanleaf.ch)

Autopilot attributes

For this action, only the serial number and device name is relevant. The hardware vendor may not be able to add the hostname, but can provide a csv file with the serial number and corresponding device (host) name. An example looks like:

csv file sample

How to rename with PowerShell

Execute the following steps to bulk rename devices according to the csv list.

  1. Install the following modules:
  • Microsoft.Graph.Intune
  • WindowsAutopilotIntune
Install-Module

2. Connect to Microsoft Graph (make sure to authenticate with an Intune admin role)

Connect-MgGraph

3. Execute the following code — essentially, it looks up the csv for the serial and sets the hostname with the modules mentioned above

Import-CSV "<yourcsvfile>.csv" | ForEach-Object {
$id = (Get-AutopilotDevice -serial $_.Serial).id

try {

Write-Host "Try to add hostname to:" $_.Computername
Set-AutopilotDevice -id $id -displayName $_.Computername

} catch {

Write-Host "Could not add hostname to:" $_.Computername "with serial:" $_.Serial -ForegroundColor red

}

}

Originally, but modified from Computernamen vergeben bei Autopilot | ictschule

Verify the hostname

It may be a good idea to verify if the hostname was successfully set on all devices. You can use the following methods to find out, which devices have no serial number set.

On command line

Use the following code: (make sure to execute steps 1 & 2 from above)

$devices = Get-AutoPilotDevice | Where-Object { $_.displayName -eq $null }

$devices | ForEach-Object {
Write-Host "Serial Number: $($_.SerialNumber)"
}

Export to csv

Also, to export to a csv, use the following:

# Get a list of all devices with no hostname set
$devices = Get-AutoPilotDevice | Where-Object { $_.displayName -eq $null }

# Define the CSV file path
$csvFilePath = "<yourcsvfilepath>"

# Create an array to store the results
$results = @()

# Iterate through the devices and add serial numbers to the results array
$devices | ForEach-Object {
$serialNumber = $_.SerialNumber
$results += [PSCustomObject]@{
"SerialNumber" = $serialNumber
}
}

# Export the results to the CSV file
$results | Export-Csv -Path $csvFilePath -NoTypeInformation

--

--