Deprovision Chromebooks with GAM by Serial Number

Have you ever needed to quickly and easily deprovision a bunch of Chromebooks. If you have a list of their serial numbers it’s a snap with GAM. You can copy and paste the code below into a local file. I chose deprovision_by_sn.sh. Be sure to update the PATH to GAM and make the file executable using the chmod u+x option.

#!/bin/bash

# Assuming the first line of the CSV is the header and serial numbers start from the second line.

INPUT="deprovision.csv"

OLDIFS=$IFS
IFS=','
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read -r SerialNumber
do
  echo "Processing $SerialNumber..."
  # Retrieve the device ID using the serial number
  deviceID=$(“PATH TO GAM”/gam print cros query "id:$SerialNumber" | sed -n '2p')
  if [ ! -z "$deviceID" ]; then
    # Deprovision the device
    “PATH TO GAM”/gam update cros "$deviceID" action deprovision_retiring_device acknowledge_device_touch_requirement
  else
    echo "No device found for Serial Number: $SerialNumber"
  fi
done < <(tail -n +2 $INPUT) # Skip the header
IFS=$OLDIFS

The script looks for the file deprovision.csv in the same directory as the script. Be sure the file begins with the header SerialNumber. Then each line can contain a serial number you wish to deprovision.