Admin SDK Directory Service Stay organized with collections Save and categorize content based on your preferences.
Page Summary
The Admin SDK Directory service in Apps Script allows administrators of Google Workspace domains to manage devices, groups, users, and other entities.
This is an advanced service that requires specific enabling steps for both the service in Apps Script and the Admin SDK on your domain.
Detailed information and reference documentation for this service are available, mirroring the public Admin SDK Directory API.
Sample code is provided demonstrating various operations like listing users and groups, getting and adding users, creating aliases, and adding group members.
The Admin SDK Directory service allows you to use the Admin SDK'sDirectory API in Apps Script. This API givesadministrators of Google Workspace domains (includingresellers) the ability tomanage devices, groups, users, and other entities in their domains.
Note: This is an advanced service that must beenabled before use. Additionally, theAdmin SDK must be enabled on your domain, as described in the API'sprerequisites documentation.Reference
For detailed information on this service, see thereference documentation for the Admin SDKDirectory API. Like all advanced services in Apps Script, the Admin SDKDirectory service uses the same objects, methods, and parameters as the publicAPI. For more information, seeHow method signatures are determined.
To report issues and find other support, see theAdmin SDK Directory support guide.
Sample code
The sample code below usesversion 1 ofthe API.
List all users
This sample lists all the users in a domain sorted by first name.
/** * Lists all the users in a domain sorted by first name. * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list */functionlistAllUsers(){letpageToken;letpage;do{page=AdminDirectory.Users.list({domain:"example.com",orderBy:"givenName",maxResults:100,pageToken:pageToken,});constusers=page.users;if(!users){console.log("No users found.");return;}// Print the user's full name and email.for(constuserofusers){console.log("%s (%s)",user.name.fullName,user.primaryEmail);}pageToken=page.nextPageToken;}while(pageToken);}
Get user
This sample gets a user by their email address and logs all of their data as aJSON string.
/** * Get a user by their email address and logs all of their data as a JSON string. * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/get */functiongetUser(){// TODO (developer) - Replace userEmail value with yoursconstuserEmail="liz@example.com";try{constuser=AdminDirectory.Users.get(userEmail);console.log("User data:\n %s",JSON.stringify(user,null,2));}catch(err){// TODO (developer)- Handle exception from the APIconsole.log("Failed with error %s",err.message);}}
Add user
This sample adds a new user to the domain, including only the requiredinformation. For the full list of user fields, see the API'sreference documentation.
/** * Adds a new user to the domain, including only the required information. For * the full list of user fields, see the API's reference documentation: * @see https://developers.google.com/admin-sdk/directory/v1/reference/users/insert */functionaddUser(){letuser={// TODO (developer) - Replace primaryEmail value with yoursprimaryEmail:"liz@example.com",name:{givenName:"Elizabeth",familyName:"Smith",},// Generate a random password string.password:Math.random().toString(36),};try{user=AdminDirectory.Users.insert(user);console.log("User %s created with ID %s.",user.primaryEmail,user.id);}catch(err){// TODO (developer)- Handle exception from the APIconsole.log("Failed with error %s",err.message);}}
Create alias
This sample creates an alias (nickname) for a user.
/** * Creates an alias (nickname) for a user. * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users.aliases/insert */functioncreateAlias(){// TODO (developer) - Replace userEmail value with yoursconstuserEmail="liz@example.com";letalias={alias:"chica@example.com",};try{alias=AdminDirectory.Users.Aliases.insert(alias,userEmail);console.log("Created alias %s for user %s.",alias.alias,userEmail);}catch(err){// TODO (developer)- Handle exception from the APIconsole.log("Failed with error %s",err.message);}}
List all groups
This sample lists all the groups in the domain.
/** * Lists all the groups in the domain. * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/groups/list */functionlistAllGroups(){letpageToken;letpage;do{page=AdminDirectory.Groups.list({domain:"example.com",maxResults:100,pageToken:pageToken,});constgroups=page.groups;if(!groups){console.log("No groups found.");return;}// Print group name and email.for(constgroupofgroups){console.log("%s (%s)",group.name,group.email);}pageToken=page.nextPageToken;}while(pageToken);}
Add group member
This sample adds a user to an existing group in the domain.
/** * Adds a user to an existing group in the domain. * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/members/insert */functionaddGroupMember(){// TODO (developer) - Replace userEmail value with yoursconstuserEmail="liz@example.com";// TODO (developer) - Replace groupEmail value with yoursconstgroupEmail="bookclub@example.com";constmember={email:userEmail,role:"MEMBER",};try{AdminDirectory.Members.insert(member,groupEmail);console.log("User %s added as a member of group %s.",userEmail,groupEmail,);}catch(err){// TODO (developer)- Handle exception from the APIconsole.log("Failed with error %s",err.message);}}
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-11 UTC.