The Problem

In large organizations, it's overwhelming for admins to find inactive users in the User List of Google Admin Console. These inactive users have not been logged in their Google accounts for a long period. It imposes a potential security risk to the organizational data if these accounts are not taken care of. The User interface does not support advanced filtering or sorting to find these inactive users, so it's fairly time consuming to find these stale accounts.

Inactive users whose last login time is a long time ago

The Solution

The following open source Google Apps Script can automate the process of printing inactive users in your domain.

Instructions

  1. Open https://script.google.com
  2. Click New project.
  3. Name the script project in the title
  4. Copy and paste the following script to the script editor, overwriting existing function myFunction() {} code
  5. Modify the inactiveDays variable to your organization settings. The default is to find any users whose last sign-in is over 30 days. You can set to a number like 60, 180, 365 etc.
  6. Click Services in the left navigation panel
    1. Select Admin SDK API
    2. Click Add
  7. Click Save Google Apps Script project button in the toolbar
  8. Click Run
  9. It will ask for your permissions in the first run
  10. After you grant the permission, the script shall run and print out the results in the Execution log.
    Execution log of printed inactive users in the domain

Source code

/**
 * Find inactive users in the domain
 * Inactive users are those whose last login time is over the given
 * amount of days. Default: 30 days.
 * 
 * Usage:
 * 1. copy and paste this source code to your Apps Script Editor
 * 2. select the following function name `findInactiveAccounts`
 * 3. In the left panel, add AdminDirectory API to the Services. 
 * 4. click 'Run'.
 * 5. The inactive users will be printed in the 'Execution log'
 *
 * © 2021 xFanatical, Inc.
 * @license MIT
 * @version 1.0.0 proof of concept
 */
const inactiveDays = 30

function findInactiveAccounts() {
  let pageToken
  let page
  const now = new Date().getTime()
  do {
    page = AdminDirectory.Users.list({
      customer: 'my_customer',
      maxResults: 100,
      pageToken,
      fields: 'users(name/fullName,primaryEmail,lastLoginTime),nextPageToken',
    })
    if (page.users.length === 0) {
      Logger.log('no user found')
    }

    page.users.forEach((user) => {
      const lastLoginTime = new Date(user.lastLoginTime).getTime()
      const diffInDays = Math.floor((now - lastLoginTime) / (1000 * 86400))
      if (lastLoginTime !== 0 && diffInDays >= inactiveDays) {
        Logger.log(`user ${user.name.fullName} <${user.primaryEmail}> is inactive for ${diffInDays} days`)
      }
    })
    pageToken = page.nextPageToken
  } while (pageToken)
}

If you're interested in extending functions or automating your tasks as much as possible on Google Workspace, please check out our code-free workflow automation software Foresight. We also provide custom apps script development services to increase your productivity. Please don't hesitate to contact us. It's our passion and expertise to assist you on Google Workspace.