Powershell Active Directory Bulk PW Reset

hi folks,

here's for sure another easy one. I like reset passwords of AD-users from a
users.txt looks like that

Z123456
Z234567

The Password has been changed to this format  C12k34!56 as you can see
in $newpassword

Could you pls help me to change the code. As far as now still not working.
appreciate for you help


if (-not (Get-Module ActiveDirectory)){ 
    Import-Module ActiveDirectory -ErrorAction Stop             
} 

$list = Get-Content -Path E:\Temp\users.txt 
 
ForEach ($u in $list) { 
$NewPassword = $u.Insert(5,"!")
$NewPassword = $newPassword.Insert(3,"k")
$NewPassword = $newPassword.Remove(0, 1)
$newPassword = $newPassword.Insert(0,"C")  

if ( -not (Get-ADUser -LDAPFilter "(sAMAccountName=$u)")) { 
Write-Host "Can't find $u" 
} 
    else {     
        $user = Get-ADUser -Identity $u 
        $user | Set-ADAccountPassword -Identity $u -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $newPassword -Force) 
        $user | Set-AdUser -ChangePasswordAtLogon $false
        write-host -ForegroundColor magenta "Password of $($u) has been set to $newPassword"
    } 

Open in new window

LVL 2
Mandy_Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

YZlatCommented:
So what your current code does is changes the password to C12k34!56 instead of Z123456? Is that the problem?
Or your code does not work period?
0
Will SzymkowskiSenior Solution ArchitectCommented:
Here is a more simplistic PS script to reset passwords using CSV.

CSV format below...

sAMAccountName       Password
bsmith                              C12k34!56
cford                                 C12k34!56
So on...

Import-Module activedirectory
$Resetpassword = Import-Csv "c:\yourCSVhere.csv"
foreach ($Account in $Resetpassword) {
$Account.sAMAccountName
$Account.Password
Set-ADAccountPassword -Identity $Account.sAMAccountName -NewPassword (ConvertTo-SecureString $Account.Password -AsPlainText -force)
}

Open in new window

0
Mandy_Author Commented:
The password should generate automatically from the samaccoutname (Z123456) to
(C12k34!56) like in the code below. If i have a list of 200 Users i cannot create
before every pw.

$NewPassword = $u.Insert(5,"!")
$NewPassword = $newPassword.Insert(3,"k")
$NewPassword = $newPassword.Remove(0, 1)
$newPassword = $newPassword.Insert(0,"C")  

Open in new window

0
Will SzymkowskiSenior Solution ArchitectCommented:
If you are using a CSV file you can type in the password and then drag it down to the bottom of the list which will popluate this password for you. You can't do that using a txt file.
0
Rainer JeschorCommented:
Hi,
simply remove the -identity parameter in the Set-ADAccountPassword:
if (-not (Get-Module ActiveDirectory)){ 
    Import-Module ActiveDirectory -ErrorAction Stop             
} 
$list = Get-Content -Path E:\Temp\users.txt 
 
ForEach ($u in $list) { 
$NewPassword = $u.Insert(5,"!") #Z123456 -> Z1234!56
$NewPassword = $newPassword.Insert(3,"k") #Z1234!56 -> Z12k34!56
$NewPassword = $newPassword.Remove(0, 1) #Z12k34!56 -> 12k34!56
$newPassword = $newPassword.Insert(0,"C")  #12k34!56 -> C12k34!56
if ( -not (Get-ADUser -LDAPFilter "(sAMAccountName=$u)")) { 
Write-Host "Can't find $u" 
} 
    else {     
        $user = Get-ADUser -Identity $u 
        $user | Set-ADAccountPassword -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $newPassword -Force) 
        $user | Set-AdUser -ChangePasswordAtLogon $false
        write-host -ForegroundColor magenta "Password of $($u) has been set to $newPassword"
    } 
}

Open in new window


HTH
Rainer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.