Hey guys, Welcome !!

You'll find some resources, reviews and other stuff that I find interesting or useful. I welcome Guest Posting, so if you are interested give me a buzz radu@thecodegene.com

Home / Resources / Google Apps / [Script] How to Force Password Change for your Google Apps users
[Script] How to Force Password Change for your Google Apps users

[Script] How to Force Password Change for your Google Apps users

Hi guys, let me share a nice script with you. Basically if for some reason you need / would like to force all your Google Apps for Work user to change their password, just use this script to do it.

function NotForceChangePasswordAtNextLogin() {
//Create logs
Logger.log(‘Execution started’);
Logger.log(‘Following users NOT forced to change password on next login’);
Logger.log(‘——————————————————–‘);

var mailSent = false;

try {
//get all users in domain — results are paginated, so it will have to use nextpageToken to get each page
var pageToken, page;
do {

//iterate for each page
page = AdminDirectory.Users.list({
domain: ‘yourdomain.com,
orderBy: ‘givenName’,
pageToken: pageToken
});
var users = page.users;
//iterate for each user — this is paginated, so this will process only the users on this page, before moving to the next page and repeating.
for (var i = 0; i < users.length; i++) {
if (users[i].primaryEmail == ‘user@yourdomain.com) { //this user is exempt from the password change
//Put the email of the user in log
Logger.log(‘*’+users[i].primaryEmail)
} else {
//set the user to change password on next login
users[i].changePasswordAtNextLogin = false;
AdminDirectory.Users.update(users[i], users[i].primaryEmail);
//Put the email of the user in log
Logger.log(users[i].primaryEmail)
}
}
pageToken = page.nextPageToken;
} while (pageToken);
}
//Catch if any error occurs
catch(e){
//Log the error
Logger.log(‘——————————————————–‘);
Logger.log(‘Error occured: ‘+e.message);
//Send an email to yourself with logs
GmailApp.sendEmail(Session.getEffectiveUser().getEmail(), ‘Log for User Pass Change Script : Error occurred’, Logger.getLog());
mailSent = true;
}

if(!mailSent){
//if execution successful
Logger.log(‘——————————————————–‘);
Logger.log(‘Execution ended. Script completed successfully’);
//Send an email to yourself with logs
GmailApp.sendEmail(Session.getEffectiveUser().getEmail(), ‘Log for User Pass Change Script: Successful’, Logger.getLog());
}

}

 

Hope this will be helpful, if you have any questions let me know and I’ll do my best to assist you. If this looks a bit to complicated you could always use GAM to do the same thing.

[mailchimp-form title=”Subscribe and get the latest posts directly in your email.” success_text=”Thank you, your email has been added to the list.” button_text=”Subscribe!” firstname=”0″ lastname=”0″]

About Radu Stoia

Self thought Digital Marketer , hobby photographer, gamer. Real life job Google Apps for Work Tech Support Specialist, in free time mostly working on my projects some travelling. I love to learn new stuff related to my interest.
Try the best Project Management platform
Scroll To Top