Powershell periodically changes user passwords

陈帅 141 Reputation points
2021-08-25T12:25:01.983+00:00

I use the following script to get an error:
Get-ADUser -SearchBase "OU=test-pc,DC=shuidi,DC=it" -Properties name,whenCreated -Filter *|Where-Object{$.whencreated -ge (Get-Date).AddDays(-1)}|Set-ADAccountPassword -Identity $.name -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "p@ssw0rd" -Force)
126335-16298941481.png

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

3 answers

Sort by: Most helpful
  1. Rich Matheisen 48,026 Reputation points
    2021-08-25T14:37:10.09+00:00

    The Identity parameter accepts these:

    A distinguished name  
    A GUID (objectGUID)  
    A security identifier (objectSid)  
    A SAM account name (sAMAccountName)  
    

    The "Name" property returned by Get-ADUser isn't acceptable. set-adaccountpassword

    0 comments No comments

  2. Limitless Technology 40,076 Reputation points
    2021-08-25T18:39:48.33+00:00

    Hello,

    This link provide you the PowerShell code steps to Set-AD Account Password with all syntax
    https://learn.microsoft.com/en-us/powershell/module/activedirectory/set-adaccountpassword?view=windowsserver2019-ps

    Hope this Answers all your queries , if not please do repost back .
    If an Answer is helpful, please click "Accept Answer" and upvote it : )

    Happy helping
    Mukesh

    0 comments No comments

  3. 陈帅 141 Reputation points
    2021-08-27T12:12:15.827+00:00

    Thank you very much for the guidance of the two teachers, adding sAMAccountName, the implementation is successful
    Get-ADUser -SearchBase "OU=test-pc,DC=shuidi,DC=it" -Properties sAMAccountName,whenCreated -Filter *|Where-Object{$.whencreated -ge (Get-Date).AddDays(-1)}|Set-ADAccountPassword -Identity $_.sAMAccountName -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "p@ssw0rd" -Force)

    0 comments No comments

Your answer

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