Bulk Add Users to Google Groups with Apps Script
Google Workspace Admins - Automate Adding Large Groups of Users to Groups
Adding users to Groups is Better with Apps Script
This is not for Apps Script N00B5. You need to know enough to use Apps Script as a Google Workspace Admin. I would either add this script to a trigger and build an automated workflow, or take it offline when it is not needed and run it manually.
If you subscribe, you can learn Apps Scrip using my multi-part Primer. The Primer is free.
Google Groups has some tools to bulk add users. If you know how to hack the uploader, you can import everyone in one batch from Google Admin.
However, building the sheet can be tedious, especially for a new organization starting from scratch.
This script has an array built in, but if you move it to a sheet or link it to a CSV file in Drive, you do not need the fixed array. The array can be dynamic and will update with new data from the source.
If you are using AI, you can quickly create the array by:
Paste the array into the AI, and state: “Study this array and do not make any changes or comments about it.”
Next, upload your CSV with these instructions: Refactor the array with this data and output a .JS file I can download
This gives you a perfect array to paste into the script each time.
Here is the JSON file, followed by the Code.
In this example, I am adding parents to a school’s Google Groups.
{
"timeZone": "America/Chicago",
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "AdminDirectory",
"version": "directory_v1",
"serviceId": "admin"
},
{
"userSymbol": "Drive",
"version": "v3",
"serviceId": "drive"
},
{
"userSymbol": "AdminGroupsSettings",
"version": "v1",
"serviceId": "groupssettings"
},
{
"userSymbol": "AdminGroupsMigration",
"version": "v1",
"serviceId": "groupsmigration"
},
{
"userSymbol": "Gmail",
"version": "v1",
"serviceId": "gmail"
}
]
},
"oauthScopes": [
"https://www.googleapis.com/auth/admin.directory.group",
"https://www.googleapis.com/auth/admin.directory.group.member",
"https://www.googleapis.com/auth/apps.groups.migration",
"https://www.googleapis.com/auth/apps.groups.settings",
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/script.send_mail"
],
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
function addNewParentsToGroups() {
// This is the email address that will receive a summary once the process is complete.
const notifyEmail = "admin@example.com";
// Here's a list of new parents and the specific Google Group each one should be added to.
// Each object in the array includes the parent's email and the name of the group.
const newParentMembers = [
{ email: "parent1@example.com", group: "2042parents@example.org" },
{ email: "parent2@example.com", group: "2026parents@example.org" },
{ email: "parent3@example.com", group: "2039parents@example.org" },
{ email: "parent4@example.com", group: "2027parents@example.org" },
{ email: "parent5@example.com", group: "2029parents@example.org" },
{ email: "parent6@example.com", group: "2041parents@example.org" },
{ email: "parent7@example.com", group: "2040parents@example.org" },
{ email: "parent8@example.com", group: "2043parents@example.org" },
{ email: "parent9@example.com", group: "2035parents@example.org" },
{ email: "parent10@example.com", group: "2038parents@example.org" }
];
// This array will collect a summary of what happens to each user (added successfully, already a member, or an error).
const addSummary = [];
// Loop through every entry in the list and try to add them to their respective group.
newParentMembers.forEach(entry => {
try {
// This is the main line that actually adds the user to the group.
AdminDirectory.Members.insert({
email: entry.email,
role: "MEMBER" // You could use "OWNER" or "MANAGER" but "MEMBER" is usually right for parents.
}, entry.group);
// Log a success message both to the log console and to the summary array.
Logger.log(`✅ Added ${entry.email} to ${entry.group}`);
addSummary.push(`✅ ${entry.email} added to ${entry.group}`);
} catch (e) {
// If the user is already in the group, we don’t want to throw a full error — just log a message and move on.
if (e.message.includes("Member already exists")) {
Logger.log(`ℹ️ ${entry.email} is already a member of ${entry.group}`);
addSummary.push(`ℹ️ ${entry.email} already in ${entry.group}`);
} else {
// If there’s any other issue (typo, group doesn’t exist, permissions problem, etc.), log it clearly.
Logger.log(`❌ Failed to add ${entry.email} to ${entry.group}: ${e.message}`);
addSummary.push(`❌ Error for ${entry.email} in ${entry.group}: ${e.message}`);
}
}
});
// Once all additions have been attempted, send a summary email so you can quickly see how it went.
MailApp.sendEmail({
to: notifyEmail,
subject: "✅ Group Additions - New Parent Members",
body: "Add Members Summary:\n\n" + addSummary.join("\n") // This just creates a nice list in the email body.
});
// Let the logs show we wrapped things up and sent the email.
Logger.log("📧 Summary email sent.");
}