Fail to add GroupTag to Autopilot devices by script

Jacky Wu 5 Reputation points
2023-08-22T15:34:02.7433333+00:00

Dears,

I would like to use script to add GroupTag to Autopilot by Serial Number, The scrip as the below:

**Connect-MgGraph

$SNs= Import-Csv "C:\NoGroupTag_Serial.csv"

foreach($SN in $SNs){

$currentdevice= Get-AutopilotDevice -Serial $SN

Set-AutopilotDevice -id $id -groupTag "ABC"

}**

However, It always got the error: **Get-AutopilotDevice : Microsoft.Graph.PowerShell.Authentication.Helpers.HttpResponseException: Response status code does not indicate success: BadRequest (Bad Request). System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord) Path C:\Add_GroupTag_by_SerialNumber.ps1:5 char: 17

  • $currentdevice= Get-AutopilotDevice -Serial $SN
    • CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
    • FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-AutopilotDevice**

If I execute the script individually by line, the success result will get. Not sure what reason system cannot recognize the value of $SN or $id. I am appreciated for your suggestion.

Microsoft Security | Windows Autopilot
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Pavel yannara Mirochnitchenko 13,391 Reputation points MVP
    2025-07-05T13:56:21.0766667+00:00

    Try to use Get-WindowsAutopilotInfoCommunity.ps1

    It is updated script to use MSGraph and supports GroupTag creation.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.