How To Create Windows Local User Account Using C#

Introduction

Here, we are going to learn to make Windows local user account using C#. We will make a C# console application and write the code in it to create user accounts via C#.

Tools

You just need Visual Studio installed on your PC to start working.

Targeted Audience

People with basic knowledge of C# are the targeted audience.

Note

You need to run Visual Studio as an administrator in order to get permission and to create Local Windows account.

What you need to do?

Checklist

  • Run Visual Studio as administrator.
  • Make a C# console application and select your targeting .NET framework.
  • Add the reference “System.DirectoryServices ” in your project.
  • Code and Run

Firstly, make a C# console application, and then, add the reference given blow to start development.

Here is the reference you need to add.

C#

Code

Code for main Program.cs file.

  1. class Program  
  2.     {  
  3.        public static string Name;  
  4.        public static string Pass;  
  5.   
  6.   
  7.         static void Main(string[] args)  
  8.         {  
  9.             Console.WriteLine("Windows Account Creator");  
  10.             Console.WriteLine("Enter User Name");  
  11.              Name = Console.ReadLine();  
  12.   
  13.             Console.WriteLine("Enter User Password");  
  14.              Pass = Console.ReadLine();  
  15.   
  16.             createUser(Name, Pass);  
  17.   
  18.         }  
  19.   
  20.         public static void createUser(string Name, string Pass) {  
  21.   
  22.   
  23.             try  
  24.             {  
  25.                 DirectoryEntry AD = new DirectoryEntry("WinNT://" +  
  26.                                     Environment.MachineName + ",computer");  
  27.                 DirectoryEntry NewUser = AD.Children.Add(Name, "user");  
  28.                 NewUser.Invoke("SetPassword"new object[] { Pass });  
  29.                 NewUser.Invoke("Put"new object[] { "Description""Test User from .NET" });  
  30.                 NewUser.CommitChanges();  
  31.                 DirectoryEntry grp;  
  32.   
  33.                 grp = AD.Children.Find("Administrators""group");  
  34.                 if (grp != null) { grp.Invoke("Add"new object[] { NewUser.Path.ToString() }); }  
  35.                 Console.WriteLine("Account Created Successfully");  
  36.                 Console.WriteLine("Press Enter to continue....");  
  37.                 Console.ReadLine();  
  38.             }  
  39.             catch (Exception ex)  
  40.             {  
  41.                 Console.WriteLine(ex.Message);  
  42.                 Console.ReadLine();  
  43.   
  44.             }  
  45.   
  46.         }  
  47.     }  

By this code, you can add user account in administrator group. If you need to add a Guest User. You can change “Administrators” to “Guest”.

Output

Output

This is the output of our console application.

Here, you see that a new local Windows account is created with the username “New User” in administrator group.

New User

Up Next
    Ebook Download
    View all
    Learn
    View all