Public/Set-ADUserPasswords.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Function Set-ADUserPasswords
{
    <#
        .SYNOPSIS
            Resets the passwords for a number of AD Users.
 
        .DESCRIPTION
            Imports data from a CSV file and will reset AD user passwords
            according to the passwords provided in the CSV. The CSV file
            should have 2 columnes at minimum, with headers 'username' and
            'password' to set the passwords on the appropriate user.
 
        .PARAMETER UserMap
            Identifies the CSV file to be used for user/password mapping.
         
        .EXAMPLE
            Set-ADUserPasswords -UserMap users.csv
         
    #>


    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true,Position=0,HelpMessage="Enter the CSV file with username & password mapping.")]
        [ValidateScript({Test-Path $_})]
        [string]
        $UserMapCSV
    )

    $UserMap = Import-Csv $UserMapCSV
    ForEach ($User in $UserMap)
    {
        Try
        {
            Set-ADAccountPassword -Identity $User.username -NewPassword (ConvertTo-SecureString -AsPlainText $User.password -Force)
            Write-Host "$($User.username) password reset to $($User.password)"
        }

        Catch
        {
            Write-Error "Failed to reset the password for user $($User.username)"
        }
    }
}