Here we'll look at how to force users to change their Microsoft 365 passwords with PowerShell.
First, I'll describe how to do it step by step from a PowerShell command line for a single account. Finally, I'll show a small script to do it for several accounts that are stored in a text file.
We'll need the Microsoft.Graph module to connect to Microsoft 365 using PowerShell.
PS C:\> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
PS C:\> Install-Module -name Microsoft.Graph
PS C:\> Connect-MgGraph
PS C:\> Update-MgUser -UserId user@shebangthedolphins.net -PasswordProfile @{ Password = "NewPassw0rd"; ForceChangePasswordNextSignIn=$false; ForceChangePasswordNextSignInWithMfa=$false }
PS C:\> Update-MgUser -UserId user@shebangthedolphins.net -PasswordProfile @{ ForceChangePasswordNextSignIn=$true; ForceChangePasswordNextSignInWithMfa=$false }
PS C:\> Get-MgUser -ConsistencyLevel eventual -Count userCount -Search '"DisplayName:cartman"'
If we want to force a large number of users to change their passwords, we can use a PowerShell script.
###########################
# author : shebangthedolphins.net
# version : 1.1
# date : 2024.11
# role : force a list of users stored in a text file to change their Microsoft 365 password
# other : create a C:\users.txt file in which to place users
# updates :
# - 1.0 (2021/03) : First Version
# - 1.1 (2024/11) : Replace obsolete MsolService with Microsoft.Graph
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Connect-MgGraph
$file = "C:\users.txt"
ForEach ($line in (Get-Content -Path $file)) {
Write-Host "Working on $line.Replace(' ','')"
#Set-MsolUserPassword -UserPrincipalName $line.Replace(' ','') -ForceChangePasswordOnly $true -ForceChangePassword $true
Update-MgUser -UserId $line.Replace(' ','') -PasswordProfile @{ ForceChangePasswordNextSignIn=$true; ForceChangePasswordNextSignInWithMfa=$false }
}
Contact :