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.
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
- Open https://script.google.com.
- Click New project.
- Name the script project in the title
- Copy and paste the following script to the script editor, overwriting existing
function myFunction() {}
code - Modify the
sourceOU
anddestinationOU
variables to match your own case. These are the OU paths in the format of/OU/SubOU/SubSubOU
. The root domain is represented by/
- Click Services in the left navigation panel
- Select Admin SDK API
- Click Add
- Click in the toolbar
- Click Run
- It will ask for your permissions in the first run
- After you grant the permission, the script shall move all your users from the
sourceOU
to thedestinationOU
.
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.