Note that there are some explanatory texts on larger screens.

plurals
  1. POChange local administrator password in C#
    text
    copied!<p>I am looking for a way to change the password of a local user account (local Administrator) on a Windows (XP in this case) machine. I have read the <a href="http://www.codeproject.com/KB/threads/Reset-Administrator-Pass.aspx" rel="noreferrer">CodeProject article</a> about one way to do this, but this just doesn't seem 'clean'. </p> <p>I can see that this is <a href="http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct04/hey1015.mspx" rel="noreferrer">possible to do with WMI</a>, so that might be the answer, but I can't figure out how to use the WinNT WMI namespace with ManagementObject. When I try the following code it throws an "Invalid Parameter" exception.</p> <pre><code>public static void ResetPassword(string computerName, string username, string newPassword){ ManagementObject managementObject = new ManagementObject("WinNT://" + computerName + "/" + username); // Throws Exception object[] newpasswordObj = {newPassword}; managementObject.InvokeMethod("SetPassword", newpasswordObj); } </code></pre> <p>Is there a better way to do this? (I'm using .NET 3.5)</p> <p><strong>Edit:</strong> Thanks Ely for pointing me in the right direction. Here is the code I ended up using:</p> <pre><code>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); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload