3 August 2004

Create a Local Windows User Account [C#/.NET]

Posted by Mikhail Esteves under: Tips; Windows .

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.



8 Comments so far...

M N Zaman Says:

14 December 2004 at 8:14 pm.

how to set flag using net user command that password never expires?

Mikhail Says:

15 December 2004 at 1:25 pm.

Hi Zaman: Type “net help user” in the command prompt to see a detailed listing of available options.

NA Says:

14 January 2005 at 12:52 am.

A suggestion: Use System.Environment.SystemDirectory instead of hard-coding the system directory path

Dan Says:

25 May 2006 at 11:55 pm.

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.

dtp Says:

24 January 2007 at 2:55 am.

Also windows paths in C# need to have the backslashes escaped as in C:\WINNT\…

Mark Lambert Says:

30 November 2007 at 3:27 pm.

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();

Peter Says:

23 January 2008 at 5:53 am.

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.

MD Says:

10 February 2008 at 6:48 am.

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.

Leave a Reply

Browse

Photography

Projects

Pages

Calendar

August 2004
M T W T F S S
« Jul   Sep »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Categories

www.flickr.com

Use OpenDNS