$users = Import-Csv .\users.csv

foreach ($user in $users) {

    $asci = [char[]]([char]48..[char]59) + ([char[]]([char]65..[char]90)) + ([char[]]([char]97..[char]122)) + ([char[]]([char]35..[char]38)) + ([char[]]([char]33))
    $password = (1..$(Get-Random -Minimum 15 -Maximum 17) | % {$asci | get-random}) -join “”
    $pw = (ConvertTo-SecureString $password -AsPlainText -Force)

    try {

        Get-ADUser $user.samaccountname | % { Set-ADAccountPassword -Identity $_.DistinguishedName -NewPassword (ConvertTo-SecureString $password -AsPlainText -Force) -Server (Get-ADDomain).PDCEmulator}

        $testlogon = (New-Object directoryservices.directoryentry "",$($user.samaccountname),$password).psbase.name -ne $null
        $Passwordlastset = ((Get-ADUser $user.samaccountname -Properties PasswordLastSet, Displayname -Server (Get-ADDomain).PDCEmulator).PasswordLastSet).tostring("yyyy-MM-dd")

    }

    catch {

    }

    $obj = new-object PSObject
    $obj | add-member -membertype NoteProperty -name "Displayname" -value $user.DisplayName
    $obj | add-member -membertype NoteProperty -name "SamAccountName" -value $user.SamAccountName
    $obj | add-member -membertype NoteProperty -name "Password" -value $password
    $obj | add-member -membertype NoteProperty -name "Testlogon" -value $testlogon
    $obj | add-member -membertype NoteProperty -name "Passwordlastset" -value $Passwordlastset
    $obj | Export-Csv -Path users_pwreset.csv -Encoding Unicode -Append -NoTypeInformation

}