Howto: Set password for user in Active Directory using Visual Basic .NET (System.DirectoryServices)

This small function can be used to set the password for a specific user in the AD.

”’ <summary>
”’ Function that sets the password for a specific user
”’ </summary>
”’ <param name=”DirEntry”>The DirectoryEntry to use eq the user</param>
”’ <param name=”sPassword”>The password</param>
”’ <returns>true/false</returns>
”’ <remarks></remarks>
Private Function AdUserSetPassword(ByVal DirEntry As DirectoryEntry, ByVal sPassword As String) As Boolean

    ‘Return false if something goes wrong
    AdUserSetPassword = False

    Try

        ‘Creating an object-”array” to store the password
        Dim _Password As Object() = New Object() {sPassword}

        ‘Calling a method
        DirEntry.Invoke(“SetPassword”, _Password)

        ‘Returning true
        AdUserSetPassword = True

    Catch ex As Exception
        ‘Throwing exception
        Throw New Exception(ex.Message, ex.InnerException)
    End Try
End Function

Leave a Comment