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.
Your pipeline doesn't work the way you expect it does.
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.
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 }