I had a need to set all members of a group so that their Office 365 (aka Microsoft Online) passwords never expire. Didn’t take too long but I though it was worth sharing. I also added output to show the setting was changed. This could certainly be prettier but it is what it is.
Pre-Reqs
- This requires you to have the Office 365 Powershell cmdlets installed, which also required the Online Services Sign-in Agent to work. See this article for instructions.
- You need admin credentials to your Office 365 account.
- The script references the ObjectID of the Office 365 Group whose members you wish to change. To get this you need to connect to Office 365 and use the Get-MsolGroup command. Below is a code snippet showing how.
import-module MSOnline Connect-MsolService Get-MsolGroup
Output will look something like below.
Script
The below would need to be saved as a .ps1 file. The Object ID (shown in red #’s below) would need to be changed to match that of the desired group
import-module MSOnline
Connect-MsolService
### Get All the Members of the Group
$agents=Get-MsolGroupMember -GroupObjectId ########-####-####-####-############
### Set PasswordNeverExpires to true for all members of the group.
Foreach ($agent in $agents ) {
Set-MsolUser -ObjectID $agent.ObjectID -PasswordNeverExpires $true
$postChangeAgent = Get-MsolUser -ObjectID $agent.ObjectID
Write-Host “User: ” $postChangeAgent.UserPrincipalName “PasswordNeverExpires:” $postChangeAgent.PasswordNeverExpires
}
Note: The line beginning with “Write Host” wraps. The end of that line in your script is the $postChangeAgent.PasswordNeverExpires
Cudos and References
Thanks to JoshT_MSFT @ the Office365 Technical Blog for the following article which pointed me in the correct direction.