The Problem

Google Admin Console can allow you move users from one organizational unit (OU) to another, but it only supports up to 50 users at a time. To move all users within that OU to another, you have to repeat the work over multiple pages. In a large organization with thousands of users, it's time-consuming drudgery obviously.

You can only change up to 50 users' organizational unit at a time in Google Admin
You can only change up to 50 users' organizational units at a time in Google Admin

The Solution

The following open source Google Apps Script can help you automate the process of moving all users in one OU to another.

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 sourceOU and destinationOU variables to match your own case. These are the OU paths in the format of /OU/SubOU/SubSubOU. The root domain is represented by /
  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 move all your users from the sourceOU to the destinationOU

Source code

/**
 * Move all users from one OU to another OU
 * 
 * Usage:
 * Change the sourceOU and destinationOU variables
 * Their format is /OU/SubOU/SubSubOU
 * The root OU is /
 * 
 * © 2021 xFanatical, Inc.
 * @license MIT
 *
 * @since 1.0.0 proof of concept
 */

const sourceOU = '/sourceOU'
const destinationOU = '/destinationOU'

function moveAllUsersToOU() {
  let pageToken
  let page
  do {
    page = AdminDirectory.Users.list({
      customer: 'my_customer',
      maxResults: 100,
      pageToken,
      query: `orgUnitPath='${sourceOU}'`,
    })
    let users = page.users
    if (users) {
      for (let i = 0; i < users.length; i++) {
        const user = users[i]
        Logger.log('Moving user %s <%s>', user.name.fullName, user.primaryEmail)
        AdminDirectory.Users.update({
          orgUnitPath: destinationOU,
        }, user.primaryEmail)
      }
    } else {
      Logger.log('No users found.')
    }
    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.