Reset password for all specified user

Introduction

The script will reset password for users in specified OUs, or in a given CSV file.

Scenarios

IT Administrators may want to reset password for a large number of users in company.  It will cost too much time to set new password one by one. Sometimes the new password should be a random password and different for every user.  It’s easy to make mistake in manually way.

Script

This module file contains three advanced function, Set-OSCADAccountPassword. You can use this script in following ways:

1.       Open Powershell in “Run as administrator” mode.

2.       Run command Import-Module “c:\ResetPassword.psm1” to import this module file.

Note Assuming you downloaded this file in the drive C directly.

Here are some code snippets for your references.

- PowerShell code snippet -
 
Write-Progress -Activity $ProgressTitle -Status "Processing" -CurrentOperation $Identity -PercentComplete ($counter / ($Userlist.Count) * 100)
                #reset password
                Set-AdaccountPassword -Identity $Identity -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $NewPassword -Force)
                #export a csv file 
                $Account = New-Object PSObject
                $Account | Add-Member -MemberType NoteProperty -Name "Identity" -Value $Identity
                $Account | Add-Member -MemberType NoteProperty -Name "NewPassword" -Value $NewPassword
                $result+=$Account
 
- end -

Examples

Example 01: How to displays help about the Set-OSCADAccountPassword function

To display help about the Set-OSCADAccountPassword function, run the following command:

                Get-Help Set-OSCADAccountPassword -Full 

  

 

Example 02: How to reset password for specified users.

 To reset password for specified users, should put these user’s SamAccountName in  a CSV file, then run the following command:

                Set-OSCADAccountPassword -path "c:\Userlist.csv"

Note first, you need to prepare a CSV file, its format appears as below. One user per line. The “Password” column is optional, if you leave it empty, this command will help you to generate a random password with 10 characters, 7 char are alphanumeric, the rest are NONalphanumeric. Both format A and format B are allowed

 

SamAccountName

Password

 SamAccountName of user, who should reset password

 The new password you want to set

SamAccountName

 SamAccountName of user, who should reset password

                Format A                                                                                 Format B

Before run this command, prepare a CSV file show as below:

Then run the command:
 Set-OSCADAccountPassword -path "c:\Userlist.csv"

Finally, a CSV file will be created on “C:\”, which contains user SamAccountName and new password.

Note default name will contain current date, you could indicate the path and file name with parameter ”–CSVPath”

Example 03: How to reset password for all users in specified OUs and their sub OU.

To reset password for all users in specified OUs and their sub OU, run the following command:
                
Set-OSCADAccountPasswordOrganizationalUnit “testou1”, "testou2" -Recurse

Note AD structure show as below

 

Example 04: How to reset password to “P@Ssw0rd” for all users in specified OU, and indicate the path of result CSV file.

To reset password to “P@Ssw0rd” for all users in OU “TestOU” and indicate the path of CSV file to “c:\report\result.csv”, run the following command:

                Set-OSCADAccountPassword –OrganizationalUnit “testou" -Password "P@Ssw0rd" –CSVPath “c:\report\result.csv”

Additional Resources

Technical Resources:

Windows PowerShell Advanced Function

http://technet.microsoft.com/en-us/library/dd315326.aspx

Get-ADOrganizationalUnit

http://technet.microsoft.com/en-us/library/ee617236.aspx

Get-ADAccountPassword

http://technet.microsoft.com/en-us/library/ee617261.aspx

Get-ADUser

http://technet.microsoft.com/en-us/library/ee617241.aspx

Export-Csv

http://technet.microsoft.com/library/hh849932.aspx