OTT-o-matic Google Console – Change Username

A long time ago I wrote a script to help a school district move from first.last@schooldistrict.org to a more privacy focused email address. They really did not want to give companies the students name simply by entering an email address. So I wrote the script and it has sat dormant for years. Well today is the day to share a refined tool that simply allows a list of usernames to be re-named and to remove the alias that keeps the old email address active. The script is quite simple, but effective. Feel free to use and modify as needed. Below is a copy of the script, but I have prepared a file for easy use. Just click here to see the template and simply the process of updating usernames.

Google Script used

//Authored by Jeran Ott. Ed.D.
//https://jeranott.com

//Note: Requires Superadmin and enabling “Admin Directory API” in “Resources Menu” under “Advance Google Service”

function changeUsername() {
var ui = SpreadsheetApp.getUi();
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(“ChangeUsername”);

//How many students by counting column A
var column = “A”;
var lastRow = sheet.getMaxRows();
var values = sheet.getRange(column + “1:” + column + lastRow).getValues();
for (; values[lastRow – 1] == “” && lastRow > 0; lastRow–) {}
var StuCount = lastRow – 1;
ui.alert(StuCount + ‘ students listed’);

//Updates each users email address
for(i=0; i<StuCount; i++) {
try{var existingEmail = sheet.getRange(i+2, 3).getValue();
var newEmail = sheet.getRange(i+2, 4).getValue();
var user = AdminDirectory.Users.get(existingEmail);
user.primaryEmail = newEmail;
AdminDirectory.Users.update(user, existingEmail);
sheet.getRange(i+2,5).setValue(“Email Changed”);

//Remove old Alias so that the original email address no longer works
AdminDirectory.Users.Aliases.remove(newEmail, existingEmail);
sheet.getRange(i+2,6).setValue(“Alias Removed”);
SpreadsheetApp.flush();<br> } catch(e) {ui.alert(“Problem renaming “+ existingEmail)}

}
ui.alert(“All Done!”);
}

Leave a Reply

Your email address will not be published.

Name *
Email *
Website