Random Thoughts

My thoughts about life, coding, science, technology and other stuff

Simple PC List from any Active Directory


  • .NET FrameWork 2.0
  • XP Professional SP2(I’ve only tested it on XP Pro SP2 but it should work on any system with the .NET framework 2.0 installed)
  • Visual C# Express or better (Actually you only need the IDE for easy of coding. You can compile this class by saving it as a .CS file and then use .NET command lines see Building from the Command Line for details).
  • Windows 2000 or higher Domain with Active Directory (tested on 2003 Domain)
  • Administrative rights on the systems if you plan to copy files, modify the registry etc

I’ve found it invaluable to be able to iterate through all the PC’s on the domain and gather information, push files, set registry keys etc. I’ve installed new programs, gathered info on the state of the systems and the possibilities are endless. With Microsoft releasing the Express Edition of C# for free, it’s so much easier for network admins who want to do more on the their networks to get started with coding.

Years ago, when I started doing work over the network, the problem was the time it takes to time out if a system is not on or unreachable. I wrote a DLL in VB6 to ping but with 2.0 version of the .NET framework, they including pinging right into the System.Net.NetworkInformation namespace. This code will get a listing of all the systems on the domain no matter what domain it’s on and then ping each one. You don’t have to pass the domain name because it uses the property defaultnamingcontext which returns the name of the domain. From there it will ping each system and then you can write code to do whatver you need. I know there are more and better ways to do this but I wanted to keep it short and sweet. Also there is very little error checking so test, test ,test and add error some more try{} catch{} statements. It’s not my responsibility if you break anything on your network so be careful! Here’s the code. Sorry for the crappy formatting but it’s the best I could do. Copy the code and save it as with a .cs extension. SciTE is a great text editor to use for editing c# code.

//Create a blank project and add the .cs file to it
//Add References to System and System.DirectoryServices

using System;
using System.Collections;
using System.DirectoryServices;
using System.Net.NetworkInformation;

class SimplePCList


     static void Main(string[] args)

          Ping pinger = new Ping();
          foreach (string pc in GetComputers())
              PingReply reply = pinger.Send(pc, 20);

               if (reply.Status == IPStatus.Success)

              //Do what you need to like:
             //Copy files to the system through the C$ share
              //Get event log entries
             //Create Registry Keys etc.Console.WriteLine(pc);

          catch (Exception err)
               Console.WriteLine(“Could not contact ” + pc + ” – ” + err.Message);



     private static ArrayList GetComputers()
         string rootdse =
     ArrayList pcs =
ADSearch(“LDAP://” + rootdse,”(objectclass=computer)”,”name”);
     return pcs;

     private static string GetObjectProperty(string ObjectPath, string ObjectProperty)
          string prop = string.Empty;
          DirectoryEntry entry = new DirectoryEntry(ObjectPath);
         prop = entry.Properties[ObjectProperty].Value.ToString();

          return prop;


     public static ArrayList ADSearch(string ConnectString, string Filter, string Attrib)
         ArrayList holding = new ArrayList();
         DirectoryEntry entry = new DirectoryEntry(ConnectString);
         DirectorySearcher DirSearcher = new      DirectorySearcher(entry);

         DirSearcher.Filter = (“(&” + Filter + “)”);
         DirSearcher.PageSize = 100;
         DirSearcher.SearchScope = SearchScope.Subtree;

         foreach (SearchResult resEnt in DirSearcher.FindAll())
              DirectoryEntry ent = resEnt.GetDirectoryEntry();


         return holding;


January 5, 2006 - Posted by | Coding

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: