How to get all Google Workspace user information within your organization

How to get all Google Workspace user information within your organization

To fetch all users of a Google Workspace domain to a Google Sheets document using Google Apps Script, you can follow these steps:

  1. Set up the script: Create a new Google Apps Script project and open the Script Editor
  2. Authorize the script: In the Script Editor, click the “Run” button and follow the prompts to authorize the script to access your Google Workspace domain.
  3. Fetch the users: Use the following code to fetch the users in your Google Workspace domain and store them in an array:
// Fetch the users in the domain
var users = AdminDirectory.Users.list({domain: 'your-domain.com'}).users
  1. Write the users to a sheet: Use the following code to write the users to a sheet in a Google Sheets document:
// Get a reference to the sheet
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

// Clear the sheet
sheet.clear();

// Write the headers to the sheet
sheet.appendRow(['Name', 'Email', 'Phone', 'Role']);

// Loop through the users and write them to the sheet
for (var i = 0; i < users.length; i++) {
  var user = users[i];
  sheet.appendRow([user.name.fullName, user.primaryEmail, user.phones[0].value, user.orgUnitPath]);}

This code will fetch all the users in your Google Workspace domain, clear the sheet, and then write the users to the sheet in the order of their name, email, phone, and role.

Keep in mind that you will need to have the appropriate permissions to access the Admin SDK and to list users in your domain. You may also need to handle pagination if your domain has a large number of users.