Create a Local Windows User Account [C#/.NET]
Using the Windows net command, it’s easy to create local Windows User Accounts. The syntax for the net command is:
net user [username] [password] /ADD
The following C# function takes in three parameters — username, password and home directory.
using System.Diagnostics;public void CreateLocalUser(string username, string password, string homedir) { if (!Directory.Exists(homedir)) Directory.CreateDirectory(homedir);Process MyProc = new Process(); MyProc.StartInfo.WorkingDirectory = @"C:\WINNT\SYSTEM32"; MyProc.StartInfo.FileName = "net.exe"; MyProc.StartInfo.UseShellExecute = false; MyProc.StartInfo.RedirectStandardError = true; MyProc.StartInfo.RedirectStandardInput = true; MyProc.StartInfo.RedirectStandardOutput = true; MyProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;MyProc.StartInfo.Arguments = @" user " + username + @" " + password + @" /ADD /ACTIVE:YES " + @"/EXPIRES:NEVER /FULLNAME:" + username + @" /HOMEDIR:""" + homedir + @""" /PASSWORDCHG:NO /PASSWORDREQ:YES";MyProc.Start(); MyProc.WaitForExit(); MyProc.Close(); }
It assumes a few settings for the user and creates a local User account. You can alter the settings to anything you want. Try “net help user” in your DOS prompt for what each of the switches mean/do.


how to set flag using net user command that password never expires?
Hi Zaman: Type “net help user” in the command prompt to see a detailed listing of available options.
A suggestion: Use System.Environment.SystemDirectory instead of hard-coding the system directory path
Seems like a hack to use another process. Why not just do it yourself?
Like this:
public static void CreateLocalUser(string username, string password, string description)
{
DirectoryEntry AD = new DirectoryEntry(“WinNT://” + Environment.MachineName + “,computer”);
DirectoryEntry NewUser = AD.Children.Add(username, “user”);
NewUser.Invoke(“SetPassword”, new object[] { password });
NewUser.Invoke(“Put”, new object[] { “Description”, description });
NewUser.CommitChanges();
}
Just my 2 cents. Other than that, it’s perfectly functional as long as net.exe exists and your Windows directory is “C:\WINNT\SYSTEM32”, which isn’t always the case.
Also windows paths in C# need to have the backslashes escaped as in C:\WINNT\…
or you can use
using System.DirectoryServices.AccountManagement;
then it is even more straight forward.
PrincipalContext pc = new PrincipalContext(ContextType.Machine);
System.DirectoryServices.AccountManagement.UserPrincipal u = new UserPrincipal(pc);
u.SetPassword(password);
u.Name = username;
u.Description = description;
u.UserCannotChangePassword = true;
u.PasswordNeverExpires = true;
u.Save();
Looks like the System.DirectoryServices.AccountManagement namespace is only available in .Net 3.5.
Dan’s CreateLocalUser method looks like pretty good option for non .Net 3.5 users.
Brilliant Mark, absolutely brilliant. Using System.DirectoryServices.AccountManagement really simplifies the process. Thanks for the code… I just had to put quotes on the username, description and password.
dtp,
Before posting on a forum, just make sure you know what you are talking about which is obviously not the case, otherwise you would know that “\” is equivalent to @”\”
The new account with Dan’s code does not come up in User Accounts in Control Panel. How can it be done?
Regarding the new user not showing up in the “Users Tab” in the control panel. This is because the user does not belong to any groups. Don’t fret this is easily accomplished. Using Marks Code here:
PrincipalContext pc = new PrincipalContext(ContextType.Machine);
System.DirectoryServices.AccountManagement.UserPrincipal u = new UserPrincipal(pc);
u.SetPassword(password);
u.Name = username;
u.Description = description;
u.UserCannotChangePassword = true;
u.PasswordNeverExpires = true;
u.Save();
Now to add this user to the “Users” group so the new entry shows up under users do this:
GroupPrincipal gPc = GroupPrincipal.FindByIdentity(pc, “Users”);
gPc.Members.Add(u);
gPc.Save();
Voila now the user shows up in the control panel.
brilliant help