PowerShell; Set Office 365 Passwords in Bulk

Recentley, I needed to set a new password for Office 365 users in bulk without Azure AD Connect. I needed to be sure that I didn’t give service accounts new passwords. In addition, this was for a multi-state organization and we wanted to set the new passwords one city at a time.

I was able to use PowerShell and the MsOnline module to meet all of the requirements. If you haven’t already, you’ll need to install the MsOnline module before running the code below. Open an elevated PowerShell console (Run as Administrator) and type Install-Module MsOnline; select Y (Yes) for any prompts.

$UserCredential = Get-Credential
       Connect-MsolService -Credential $UserCredential

$City = Read-Host "Enter city's name to set a new password for all users in that location"
$NewPassword = Read-Host "Enter new password for all user's in the specified city"

$O365_Users = Get-MsolUser -All| where {($_.city -eq $City) -and ($_.isLicensed -like "True")}|select UserPrincipalName
       foreach ($user in $O365_Users)
                 Get-msoluser -UserPrincipalName $User.UserPrincipalName |set-msoluserpassword -newpassword $NewPassword -forcechangepassword $false

When you run the script it will prompt you for your Office 365 admin credentials, the city, and the new password that you would like to specify. You can force a password change after the first logon by chainging $false to $true.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s