How to list all  computers in a given active directory site

Posted on 2009-04-14
Last Modified: 2012-05-06
Is there an easy way of showing all the computers currently in each active directory site within a domain ?
Question by:drlargepants
  • 4
  • 3
LVL 70

Expert Comment

by:Chris Dent
ID: 24139294

It's not amazingly easy, you have to grab the list of subnets and do a bit of IP math to figure out if each computer is part of that subnet (typically based on a lookup of a name in DNS).

You have a lot of them?


Author Comment

ID: 24139336
A couple of 100 ?!  Its from an NT 4 migration, so ideally we need to know what is where and then create an OU for each site.
LVL 70

Expert Comment

by:Chris Dent
ID: 24139394

Do you have a list of your IP ranges (network address and subnet mask)? And a list of the computers?

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why


Author Comment

ID: 24139531
Yeah can get that easy enough from angryip
LVL 70

Expert Comment

by:Chris Dent
ID: 24139578

Then it's not so hard to do, I have a pile of VbScript subroutines for doing IP math and have done this kind of operation before for a much bigger domain.

It's reliant on being able to resolve the computer names to IP addresses, but I guess your infrastructure already allows that?


Author Comment

ID: 24139791
yeah DNS is good to go :)
LVL 70

Accepted Solution

Chris Dent earned 20 total points
ID: 24146379

Cool, in that case we can have a play with this one. I've based this on files looking like this:




It is possible to read the subnets from Active Directory instead of a file if they're configured there and you'd rather use those.

The file it creates a is a CSV file, you should be able to sort it by the Network Address field (in Excel) which will give you a break down of PCs within a given site. If we read subnets from AD we can add the site name as well.


Option Explicit

Const SUBNET_LIST = "subnets.txt"

Const COMPUTER_LIST = "computers.txt"

Const REPORT_FILE = "report.csv"

Function GetDNSAddresses(strComputerName)

  ' Return Type: Array


  ' Returns an Array containing each IP Address for a given Node Name from a

  ' DNS Query

  Dim objShell : Set objShell = CreateObject("WScript.Shell")

  Dim objScriptExec : Set objScriptExec = objShell.Exec("nslookup " & strComputerName)

  Dim objStdOut : Set objStdOut = objScriptExec.StdOut


  Dim strIPAddress : strIPAddress = ""

  Do Until objStdOut.AtEndOfStream

    Dim strOut : strOut = objStdOut.ReadLine

    If InStr(strOut, "Name:") Then

      strIPAddress = objStdOut.ReadLine


      If InStr(strIPAddress, "Address:") > 0 Then

        strIPAddress = Trim(Replace(strIPAddress, "Address:", ""))

      ElseIf InStr(strIPAddress, "Addresses:") > 0 Then

        strIPAddress = Trim(Replace(strIPAddress, "Addresses:", ""))

      End If

    End If



  GetDNSAddresses = Split(strIPAddress, ", ")


  Set objStdOut = Nothing

  Set objScriptExec = Nothing

  Set objShell = Nothing

End Function

Function ConvertIPToBinary(strIP)

  ' Converts an IP Address into Binary


  Dim arrOctets : arrOctets = Split(strIP, ".")

  Dim i

  For i = 0 to UBound(arrOctets)

    Dim intOctet : intOctet = CInt(arrOctets(i))

    Dim strBinOctet : strBinOctet = ""

    Dim j

    For j = 0 To 7

      If intOctet And (2^(7 - j)) Then

        strBinOctet = strBinOctet & "1"


        strBinOctet = strBinOctet & "0"

      End If


    arrOctets(i) = strBinOctet


  ConvertIPToBinary = Join(arrOctets, ".")

End Function

Function ConvertBinIPToDecimal(strBinIP)

  ' Convert binary form of an IP back to decimal

  Dim arrOctets : arrOctets = Split(strBinIP, ".")

  Dim i

  For i = 0 to UBound(arrOctets)

    Dim intOctet : intOctet = 0

    Dim j

    For j = 0 to 7

      Dim intBit : intBit = CInt(Mid(arrOctets(i), j + 1, 1))

      If intBit = 1 Then

        intOctet = intOctet + 2^(7 - j)

      End If


    arrOctets(i) = CStr(intOctet)



  ConvertBinIPToDecimal = Join(arrOctets, ".")

End Function

Function MaskLengthToIP(intMask)

  ' Converts a mask length to the decimal format mask

  Dim intFullOctets : intFullOctets = (intMask - (intMask Mod 8)) / 8

  Dim i : Dim arrOctets(3)

  For i = 0 To (intFullOctets - 1)

    arrOctets(i) = "255"



  Dim intPartialOctetLen : intPartialOctetLen = intMask Mod 8

  Dim j, intOctet

  If intPartialOctetLen > 0 Then

    For j = 0 To (intPartialOctetLen - 1)

      intOctet = intOctet + 2^(7 - j)


    arrOctets(i) = intOctet : i = i + 1

  End If


  For j = i To 3

    arrOctets(j) = "0"



  MaskLengthToIP = Join(arrOctets, ".")

End Function

Function CalcNetworkAddress(strIP, strMask)

  ' Generates the Network Address from the IP and Mask

  ' Conversion of IP and Mask to binary

  Dim strBinIP : strBinIP = ConvertIPToBinary(strIP)

  Dim strBinMask : strBinMask = ConvertIPToBinary(strMask)


  ' Bitwise AND operation (except for the dot)

  Dim i, strBinNetwork

  For i = 1 to Len(strBinIP)

    Dim strIPBit : strIPBit = Mid(strBinIP, i, 1)

    Dim strMaskBit : strMaskBit = Mid(strBinMask, i, 1)


    If strIPBit = "1" And strMaskBit = "1" Then

      strBinNetwork = strBinNetwork & "1"

    ElseIf strIPBit = "." Then

      strBinNetwork = strBinNetwork & strIPBit


      strBinNetwork = strBinNetwork & "0"

    End If



  ' Conversion of Binary IP to Decimal

  CalcNetworkAddress= ConvertBinIPToDecimal(strBinNetwork)

End Function


' Main Code


Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim objSubnetFile : Set objSubnetFile = objFSO.OpenTextFile(SUBNET_LIST, 1, False, 0)

Dim objSubnetList : Set objSubnetList = CreateObject("Scripting.Dictionary")

Do While Not objSubnetFile.AtEndOfStream

  Dim arrTemp : arrTemp = Split(objSubnetFile.ReadLine, "/")

  Dim strNetworkAddress : strNetworkAddress = arrTemp(0)

  Dim strMaskLength : strMaskLength = arrTemp(1)

  objSubnetList.Add strNetworkAddress, strMaskLength


Set objSubnetFile = Nothing

Dim objComputerFile : Set objComputerFile = objFSO.OpenTextFile(COMPUTER_LIST, 1, False, 0)

Dim objReportFile : Set objReportFile = objFSO.OpenTextFile(REPORT_FILE, 2, True, 0)

objReportFile.WriteLine "Client Name,Client IP,Network Address,Subnet Mask"

Do While Not objComputerFile.AtEndOfStream

  Dim strClientName : strClientName = objComputerFile.ReadLine

  Dim arrClientIPs : arrClientIPs = GetDNSAddresses(strClientName)

  Dim strClientIP

  For Each strClientIP in arrClientIPs

    For Each strNetworkAddress in objSubnetList

      Dim strIPMask : strIPMask = MaskLengthToIP(objSubnetList(strNetworkAddress))

      If strNetworkAddress = CalcNetworkAddress(strClientIP, strIPMask) Then

        objReportFile.WriteLine strClientName & "," & strClientIP & "," & strNetworkAddress & "," & strIPMask

        Exit For

      End If




Open in new window


Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get all machines that a user is logged onto 1 53
Issue with server 2012 R2 and access to folders 20 42
Federation ID format? 3 31
edit user account 1 29
Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now