Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to run a script that modify password for multiple AD user accounts, enable the accounts and force a password change at next logon.

I use this code but that's not work :

Get-ADUSER -Filter * -SearchScope Subtree -SearchBase "OU=myou,OU=otherou,DC=mydc,DC=local" | Set-ADAccountPassword -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "NewPassord" -Force) | Enable-ADAccount | Set-ADUSER -ChangePasswordAtLogon $true

If I run the Get-ADuser line with ONLY one of the other line that's run fine ex :

Get-ADUSER -Filter * -SearchScope Subtree -SearchBase "OU=myou,OU=otherou,DC=mydc,DC=local" | Enable-ADAccount

Where I'm wrong ? I'm new to PowerShell probably I'm misunderstanding something.

share|improve this question
up vote 4 down vote accepted

Your pipeline doesn't work the way you expect it does.

$UserList=Get-ADUSER -Filter * -SearchScope Subtree -SearchBase "OU=myou,OU=otherou,DC=mydc,DC=local"
foreach ($User in $UserList) {
    Set-ADAccountPassword $User -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "NewPassord" -Force
    Enable-ADAccount $User
    Set-ADUSER $user -ChangePasswordAtLogon $true

Pipelines aren't good for multiple actions on the same object. They only work if each command in the pipeline forwards the same object as the initial object. For that, iterating through a loop makes a lot more sense.

share|improve this answer
I think the last instruction should be Set-ADUser $User -ChangePasswordAtLogon $true. – Ansgar Wiechers Nov 1 '12 at 21:58
Works!! Thanks a lot! – Patrick Pellegrino Nov 2 '12 at 15:12

Get-ADUSER -Filter * -SearchScope Subtree -SearchBase "OU=myou,OU=otherou,DC=mydc,DC=local" | %{ Set-ADAccountPassword $.Name -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "NewPassord" -Force) Enable-ADAccount $.Name Set-ADUSER $_.Name-ChangePasswordAtLogon $true }

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.