Admin SDK Directory Service
Stay organized with collections
Save and categorize content based on your preferences.
The Admin SDK Directory service allows you to use the Admin SDK's
Directory API in Apps Script. This API gives
administrators of Google Workspace domains (including
resellers) the ability to
manage devices, groups, users, and other entities in their domains.
Reference
For detailed information on this service, see the
reference documentation for the Admin SDK
Directory API. Like all advanced services in Apps Script, the Admin SDK
Directory service uses the same objects, methods, and parameters as the public
API. For more information, see How method signatures are determined.
To report issues and find other support, see the
Admin SDK Directory support guide.
Sample code
The sample code below uses version 1 of
the API.
List all users
This sample lists all the users in a domain sorted by first name.
Get user
This sample gets a user by their email address and logs all of their data as a
JSON string.
Add user
This sample 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.
Create alias
This sample creates an alias (nickname) for a user.
List all groups
This sample lists all the groups in the domain.
Add group member
This sample adds a user to an existing group in the domain.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-28 UTC.
[null,null,["Last updated 2025-08-28 UTC."],[[["\u003cp\u003eThe Admin SDK Directory service enables Google Workspace administrators to manage domain resources like users, groups, and devices within Apps Script using the Directory API.\u003c/p\u003e\n"],["\u003cp\u003eThis advanced service requires enabling both the Admin SDK and the specific service before use, and it mirrors the functionality of the public Directory API.\u003c/p\u003e\n"],["\u003cp\u003eSample code snippets demonstrate common tasks like listing and managing users, creating aliases, and handling groups and their members via the Admin SDK Directory service.\u003c/p\u003e\n"],["\u003cp\u003eBefore using the sample code, remember to enable the Admin SDK, replace placeholder values with your specific data, and refer to the documentation for details on API usage and error handling.\u003c/p\u003e\n"]]],[],null,["# Admin SDK Directory Service\n\nThe Admin SDK Directory service allows you to use the Admin SDK's\n[Directory API](/admin-sdk/directory) in Apps Script. This API gives\nadministrators of Google Workspace domains (including\nresellers) the ability to\nmanage devices, groups, users, and other entities in their domains.\n| **Note:** This is an advanced service that must be [enabled before use](/apps-script/guides/services/advanced). Additionally, the Admin SDK must be enabled on your domain, as described in the API's [prerequisites documentation](/admin-sdk/directory/v1/guides/prerequisites).\n\nReference\n---------\n\nFor detailed information on this service, see the\n[reference documentation](/admin-sdk/directory/v1/reference) for the Admin SDK\nDirectory API. Like all advanced services in Apps Script, the Admin SDK\nDirectory service uses the same objects, methods, and parameters as the public\nAPI. For more information, see [How method signatures are determined](/apps-script/guides/services/advanced#how_method_signatures_are_determined).\n\nTo report issues and find other support, see the\n[Admin SDK Directory support guide](/admin-sdk/directory/support).\n\nSample code\n-----------\n\nThe sample code below uses [version 1](/admin-sdk/directory/v1/reference) of\nthe API.\n\n### List all users\n\nThis sample lists all the users in a domain sorted by first name. \nadvanced/adminSDK.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/adminSDK.gs) \n\n```javascript\n/**\n * Lists all the users in a domain sorted by first name.\n * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list\n */\nfunction listAllUsers() {\n let pageToken;\n let page;\n do {\n page = AdminDirectory.Users.list({\n domain: 'example.com',\n orderBy: 'givenName',\n maxResults: 100,\n pageToken: pageToken\n });\n const users = page.users;\n if (!users) {\n console.log('No users found.');\n return;\n }\n // Print the user's full name and email.\n for (const user of users) {\n console.log('%s (%s)', user.name.fullName, user.primaryEmail);\n }\n pageToken = page.nextPageToken;\n } while (pageToken);\n}\n```\n\n### Get user\n\nThis sample gets a user by their email address and logs all of their data as a\nJSON string. \nadvanced/adminSDK.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/adminSDK.gs) \n\n```javascript\n/**\n * Get a user by their email address and logs all of their data as a JSON string.\n * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/get\n */\nfunction getUser() {\n // TODO (developer) - Replace userEmail value with yours\n const userEmail = 'liz@example.com';\n try {\n const user = AdminDirectory.Users.get(userEmail);\n console.log('User data:\\n %s', JSON.stringify(user, null, 2));\n } catch (err) {\n // TODO (developer)- Handle exception from the API\n console.log('Failed with error %s', err.message);\n }\n}\n```\n\n### Add user\n\nThis sample adds a new user to the domain, including only the required\ninformation. For the full list of user fields, see the API's\n[reference documentation](/admin-sdk/directory/v1/reference/users/insert). \nadvanced/adminSDK.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/adminSDK.gs) \n\n```javascript\n/**\n * Adds a new user to the domain, including only the required information. For\n * the full list of user fields, see the API's reference documentation:\n * @see https://developers.google.com/admin-sdk/directory/v1/reference/users/insert\n */\nfunction addUser() {\n let user = {\n // TODO (developer) - Replace primaryEmail value with yours\n primaryEmail: 'liz@example.com',\n name: {\n givenName: 'Elizabeth',\n familyName: 'Smith'\n },\n // Generate a random password string.\n password: Math.random().toString(36)\n };\n try {\n user = AdminDirectory.Users.insert(user);\n console.log('User %s created with ID %s.', user.primaryEmail, user.id);\n } catch (err) {\n // TODO (developer)- Handle exception from the API\n console.log('Failed with error %s', err.message);\n }\n}\n```\n\n### Create alias\n\nThis sample creates an alias (nickname) for a user. \nadvanced/adminSDK.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/adminSDK.gs) \n\n```javascript\n/**\n * Creates an alias (nickname) for a user.\n * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users.aliases/insert\n */\nfunction createAlias() {\n // TODO (developer) - Replace userEmail value with yours\n const userEmail = 'liz@example.com';\n let alias = {\n alias: 'chica@example.com'\n };\n try {\n alias = AdminDirectory.Users.Aliases.insert(alias, userEmail);\n console.log('Created alias %s for user %s.', alias.alias, userEmail);\n } catch (err) {\n // TODO (developer)- Handle exception from the API\n console.log('Failed with error %s', err.message);\n }\n}\n```\n\n### List all groups\n\nThis sample lists all the groups in the domain. \nadvanced/adminSDK.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/adminSDK.gs) \n\n```javascript\n/**\n * Lists all the groups in the domain.\n * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/groups/list\n */\nfunction listAllGroups() {\n let pageToken;\n let page;\n do {\n page = AdminDirectory.Groups.list({\n domain: 'example.com',\n maxResults: 100,\n pageToken: pageToken\n });\n const groups = page.groups;\n if (!groups) {\n console.log('No groups found.');\n return;\n }\n // Print group name and email.\n for (const group of groups) {\n console.log('%s (%s)', group.name, group.email);\n }\n pageToken = page.nextPageToken;\n } while (pageToken);\n}\n```\n\n### Add group member\n\nThis sample adds a user to an existing group in the domain. \nadvanced/adminSDK.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/adminSDK.gs) \n\n```javascript\n/**\n * Adds a user to an existing group in the domain.\n * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/members/insert\n */\nfunction addGroupMember() {\n // TODO (developer) - Replace userEmail value with yours\n const userEmail = 'liz@example.com';\n // TODO (developer) - Replace groupEmail value with yours\n const groupEmail = 'bookclub@example.com';\n const member = {\n email: userEmail,\n role: 'MEMBER'\n };\n try {\n AdminDirectory.Members.insert(member, groupEmail);\n console.log('User %s added as a member of group %s.', userEmail, groupEmail);\n } catch (err) {\n // TODO (developer)- Handle exception from the API\n console.log('Failed with error %s', err.message);\n }\n}\n```"]]