How to get all Google Workspace user information within your organization

How to get all Google Workspace user information within your organization

Google Workspace allows you to download your organization account’s list of users, and create a list that is filtered by user attribute, like active status or suspended status, or by your organization. You can download a comma-separated value file (user’s list), and then export this file to a spreadsheet in Google Sheets. 

In this article, we will be telling you all about how to fetch Google Workspace domain users using the Google Apps Script. The code will help you fetch all Google Workspace domain users, clear the sheet, and write users in Google Sheets according to their name, phone, address, and role. 

However, it is important to note that appropriate permission access to Admin SDK and users in the domain list is essential. In case, your domain has a large number of users, then you will also have to handle pagination.

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: ''}).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

// 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.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.