Powershell script is not resetting user password

T Crha 396 Reputation points
2023-03-01T12:55:39.32+00:00

Hello everyone,

I am wondering if there is someone here able to help me with a strange thing I came across while executing a PS script that creates AD users in our organization.

The part I need to help with in particular is the one where newcomers password is being set up. Within the script, it looks like this:

$username_new = <samaccountname of the newcomer>
$random = -join ((0..9) | Get-Random -Count 4)
$ADpwdtext = "text1" + $random + "text2"
$dc = <domain controller>
#$sessionAD = New-PSSession -ComputerName $dc -Credential $cred

Set-ADAccountPassword -Identity $username_new -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "$ADpwdtext" -Force) -Server $dc -Confirm:$True;
Set-ADUser -Identity $username_new -ChangePasswordAtLogon:$true -Server $dc -Confirm:$True;
get-aduser $username_new -Properties * | select password*

Write-Host "New password is:" -ForegroundColor Yellow
Write-Host "$ADpwdtext`n`n" -ForegroundColor Black -BackgroundColor Yellow
#endregion

But when I run this part of the script, it doesnt do anything - neither does it set the password, nor does it set the password to be reset upon next logon. When I check users password properties after that, it looks like this:

get-aduser <samaccountname of the newcomer> -Properties * | select password*

PasswordExpired PasswordLastSet PasswordNeverExpires PasswordNotRequired

   True                                False               False

When I run those Set cmdlets separately, they work without any problems.

Any clues why this is happening?

Thanks everyone!

Tomas

Windows for business | Windows Server | User experience | PowerShell
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.