Using PowerShell to change user password at first logon

Step 1: Identify users who need to change their password at first logon

Retrieve users who have not changed their password since account creation.

Get-ADUser -Filter {PasswordLastSet -eq 0} -Properties SamAccountName

Step 2: Force password change at next logon

Set 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
}

Step 3: Apply to a specific user or group

To enforce this setting for a single user, run the script below.

Set-ADUser -Identity "JohnDoe" -ChangePasswordAtLogon $true

To apply this for an entire group, run the script below.

Get-ADGroupMember -Identity "NewEmployees" | ForEach-Object { Set-ADUser -Identity $_.SamAccountName -ChangePasswordAtLogon $true }

Step 4: Automate with a scheduled task

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"

FAQs

1. How do I force a user to change their password at the next login?

Run the script below to force a user to change their password at the next login.

Set-ADUser -Identity username -ChangePasswordAtLogon $true

2. Can I apply this setting in bulk for multiple users?

Yes, run the script below to apply this setting in bulk for multiple users.

Get-ADUser -Filter * | Set-ADUser -ChangePasswordAtLogon $true

3. How do I verify if a user must change their password at the next login?

Run the script below to verify if a user must change their password at the next login.

Get-ADUser -Identity username -Properties ChangePasswordAtLogon
 
  • Step 1: Identify users who need to change their password at first logon
  • Step 2: Force password change at next logon
  • Step 3: Apply to a specific user or group
  • Step 4: Automate with a scheduled task
  • FAQs

ADSelfService Plus trusted by

A single pane of glass for complete self service password management
Email Download Link