'Can not set a user password using Powershell

I am trying to create a user with a specific password using powershell. The below code creates the user but then gives me a rejected credentials error when I attempt to search AD using this new user. Similarly a runas command tells me the password is bad. I have also tried the Set-ADAccountPassword cmdlet after creating the user and I get no error but I can confirm that I am not actually setting the password. I also can not find a password reset Event when I do this. When I use the "Users and Computers" UI, I am able to reset the password on this user properly (and I see the Event). I am running the code on the DC as a Domain Admin with 'Run as Administrator'.

I set up a test Domain and the below code works properly so it is something screwy with my domain. I have tried removing all password complexity requirements as a test but no luck. Ideas on other things to check?

$Password="pa55Word!"
$Name="Tmp.User"
[securestring]$SecPass =  ConvertTo-SecureString -String $Password -AsPlainText -Force

New-ADUser -Name $Name -AccountPassword $SecPass -Enabled $True -ChangePasswordAtLogon $False

$Credential = New-Object System.Management.Automation.PSCredential $Name, $SecPass
Get-ADUser -Filter 'Name -eq $Name' -Credential $Credential


Solution 1:[1]

I have tested in my environment

I ran the same script and I am able to get the user details with new user credentials

I am able to the set the password using Set-ADAccountPassword cmdlet

The issue might be with your domain or from the DC where you are running the script

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RamaraoAdapa-MT