Search code examples
c#wmi

Change user password on remote computer using WMI


Is there a way to change a users password on a remote computer using WMI? I couldn't locate any resources on this.

I'd just like to add that we are not using active directory and I need to write my code in C#.


Solution

  • Well, these are VB Script examples in this Hey Scripting Guy column, but they should be translatable:

    How do I change the local Administrator password for all the computers in an OU?

    Set objOU = GetObject("LDAP://OU=Finance, DC=fabrikam, DC=com")
    objOU.Filter = Array("Computer")
    
    For Each objItem in objOU
        strComputer = objItem.CN
        Set objUser = GetObject("WinNT://" & strComputer & "/Administrator")
        objUser.SetPassword("i5A2sj*!")
    Next
    

    The first part is AD based, but is just being used to find all of the machines in the domain. The second part (that does the actual remote password reset) doesn't rely on AD at all.


    So, it's basically bind to WinNT://<ComputeName>/<UserName>, then call SetPassword().


    And this other SO question on changing the local admin account password is already in C#:

    public static void ResetPassword(string computerName, string username, string newPassword) { 
            DirectoryEntry directoryEntry = new DirectoryEntry(string.Format("WinNT://{0}/{1}", computerName, username)); 
            directoryEntry.Invoke("SetPassword", newPassword);
    }