Tiny Tips 4 : Adding Users in Bulk for Windows XP

Recently I came across a requirement where it was needed to create user accounts in bulk from a CSV file. Initially I thought there should be a ready made option present for doing this from command line and was happy to locate a KB article about AddUsers.exe, but it seems that the AddUsers.exe file is only available for Windows NT server and Windows 2000 Server operating systems.

A bit more digging into the available commands in Windows XP brought up net user, though this is not a utility for creating user accounts in bulk it provides an option to create user accounts quickly. So I hacked in a command line application which will accept a CSV file and execute the net user command internally.

Source follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
 
namespace BulkAddUsers {
    class Program {
        static int Main(string[] args) {
            if (args.Length <1) {
                Console.WriteLine("[Usage]");
                Console.WriteLine("BulkAddUsers <filename> [<character>]");
                Console.WriteLine("<filename> is the name of the file which contains the user name and password of the users in CSV format");
                Console.WriteLine("<character> is the separatation character used in the CSV file (comma (,) , semicolon (;) , space ( ))");
                return -1;
            }
 
            string csvFile = args[0];
            char sepChar = ',';
 
            if (args.Length> 1) {
                sepChar = args[1][0];
            }
 
           
 
            string csvContents = string.Empty;
 
            using (StreamReader sr = new StreamReader(csvFile)) {
                while ((csvContents = sr.ReadLine()) != null) {
                    //Split on sepChar
                    string[] tokens = csvContents.Split(sepChar);
 
                    string command = "net";
                    string arguments = "user " + tokens[0] + " "+ tokens[1] +" /add";
 
                    Process p = new Process();
                    p.StartInfo.FileName = command;
                    p.StartInfo.Arguments = arguments;
                    p.StartInfo.CreateNoWindow = true;
 
                    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
 
                    p.Start();
                    p.WaitForExit();
 
                    if (p.ExitCode != 0) {
                        Console.WriteLine("Error creating user {0}, return code {1}", tokens[0], p.ExitCode);
                    }
                }
            }
 
            return 0;
        }
    }
}
comments powered by Disqus