The below scripts can force users belonging to specific OUs to change their passwords during their next logon. Often, using PowerShell can be complex and time consuming.
ADSelfService Plus, an Active Directory self-service password management and single sign-on solution, offers an option that, when enabled, forces users to change their password every time they logon after a password reset (self-service or automated password reset).
Here is a comparison between forcing users to change their passwords using PowerShell and ADSelfService Plus:
Enter the following PowerShell script to force the users belonging to a specific OU to change their passwords during the next logon:
Get-ADUser -Filter * -SearchBase “OU=<specify-OU-name>,DC=<specify-subdomain-name>,DC=<specify-domain-name>,DC=com” | Set-ADUser -CannotChangePassword:$false -PasswordNeverExpires:$false -ChangePasswordAtLogon:$trueIn ADselfService Plus:
Retrieve users who have not changed their password since account creation.
Get-ADUser -Filter {PasswordLastSet -eq 0} -Properties SamAccountNameSet the flag for all identified users. This ensures users are prompted to reset their password when they log in.
foreach ($user in (Get-ADUser -Filter {PasswordLastSet -eq 0})) {
Set-ADUser -Identity $user.SamAccountName -ChangePasswordAtLogon $true
}To enforce this setting for a single user, run the script below.
Set-ADUser -Identity "JohnDoe" -ChangePasswordAtLogon $trueTo apply this for an entire group, run the script below.
Get-ADGroupMember -Identity "NewEmployees" | ForEach-Object { Set-ADUser -Identity $_.SamAccountName -ChangePasswordAtLogon $true }To automatically enforce this rule for new users daily, run the script below. This automates the process every morning at 6am.
$script = "Get-ADUser -Filter {PasswordLastSet -eq 0} | ForEach-Object { Set-ADUser -Identity $_.SamAccountName -ChangePasswordAtLogon $true }" $trigger = New-ScheduledTaskTrigger -Daily -At "06:00AM" $action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:\Scripts\ForcePasswordChange.ps1" Register-ScheduledTask -TaskName "ForcePasswordReset" -Trigger $trigger -Action $action -User "Administrator" -Password "adminpassword"Run the script below to force a user to change their password at the next login.
Set-ADUser -Identity username -ChangePasswordAtLogon $trueYes, run the script below to apply this setting in bulk for multiple users.
Get-ADUser -Filter * | Set-ADUser -ChangePasswordAtLogon $trueRun the script below to verify if a user must change their password at the next login.
Get-ADUser -Identity username -Properties ChangePasswordAtLogon