views:

179

answers:

1

How do I change a local user account password remotely using VB.NET/C#?

I have looked into the DirectoryEntry class and know how to add users to a group but cannot figure out how to change a (local) password.

+2  A: 

Using .net 3.5:

PrincipalContext context = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.FindByIdentity(context, "user");

The user can change her own password:

user.ChangePassword("old", "new");

Or, if you run as AD administrator, you can reset it:

user.SetPassword("1234567");
Kobi
I think the OP wants to change passwords for non-domain users on a remote machine.
klausbyskov
In that case, I believe `ContextType.Machine` should work. Not quite sure, never dealt with local users. Thanks.
Kobi
Yes, I am looking for a way to modify local (i.e. non-domain) users. I'll try ContextType.Machine, but how will I tell it which machine to use?
Andrew J. Brehm
Ta, this worked. Turns out UserPrincipal.FindByIdentity(context, strMyServerName) with context being a ContextType.Machine worked.
Andrew J. Brehm