VBScript

Mapping Your AD: VBScript to List OUs in Parent-Child Order | Lazy Admin Blog

Posted on Updated on

When you’re managing a complex Active Directory environment, getting a clear “birds-eye view” of your structure is essential. While the Active Directory Users & Computers (dsa.msc) snap-in is great for manual navigation, sometimes you need a flat text output that preserves the visual hierarchy of your Organizational Units (OUs).

The following VBScript crawls your LDAP directory and mirrors the parent-child nesting you see in your GUI tools.


📜 The Script: ListAllOUs_ParentChild.vbs

Copy the code below and save it as ListAllOUs_ParentChild.vbs.

VBScript
Option Explicit
Const ADS_SCOPE_SUBTREE = 2
Dim ObjConn, ObjRS, ObjRootDSE
Dim StrSQL, StrDomName, ObjOU
' Get the local domain name
Set ObjRootDSE = GetObject("LDAP://RootDSE")
StrDomName = Trim(ObjRootDSE.Get("DefaultNamingContext"))
Set ObjRootDSE = Nothing
' SQL Query to find OUs (Excluding Domain Controllers)
StrSQL = "Select Name, ADsPath From 'LDAP://" & StrDomName & "' Where ObjectCategory = 'OrganizationalUnit' And Name <> 'Domain Controllers'"
Set ObjConn = CreateObject("ADODB.Connection")
ObjConn.Provider = "ADsDSOObject"
ObjConn.Open "Active Directory Provider"
Set ObjRS = CreateObject("ADODB.Recordset")
ObjRS.Open StrSQL, ObjConn
If Not ObjRS.EOF Then
ObjRS.MoveLast: ObjRS.MoveFirst
WScript.Echo vbNullString
WScript.Echo "Total OU: " & Trim(ObjRS.RecordCount)
WScript.Echo "==================="
WScript.Echo vbNullString
While Not ObjRS.EOF
Set ObjOU = GetObject(Trim(ObjRS.Fields("ADsPath").Value))
' Check if it's a top-level Parent OU
If StrComp(Right(Trim(ObjOU.Parent), Len(Trim(ObjOU.Parent)) - 7), StrDomName, VbTextCompare) = 0 Then
WScript.Echo "Parent OU: " & Trim(ObjRS.Fields("Name").Value)
GetChild(ObjOU)
End If
ObjRS.MoveNext
Set ObjOU = Nothing
Wend
End If
ObjRS.Close: Set ObjRS = Nothing
ObjConn.Close: Set ObjConn = Nothing
' Subroutine to find first-level children
Private Sub GetChild(ThisObject)
Dim ObjChild
For Each ObjChild In ThisObject
If StrComp(Trim(ObjChild.Class), "OrganizationalUnit", VbTextCompare) = 0 Then
WScript.Echo vbTab & ">> Child OU: " & Right(Trim(ObjChild.Name), Len(Trim(ObjChild.Name)) - 3)
GetGrandChild (ObjChild.ADsPath)
End If
Next
End Sub
' Recursive subroutine to find all nested children
Private Sub GetGrandChild (ThisADsPath)
Dim ObjGrand, ObjItem
Set ObjGrand = GetObject(ThisADsPath)
For Each ObjItem In ObjGrand
If StrComp(Trim(ObjItem.Class), "OrganizationalUnit", VbTextCompare) = 0 Then
WScript.Echo vbTab & vbTab & ">> Child OU: " & Right(Trim(ObjItem.Name), Len(Trim(ObjItem.Name)) - 3)
GetGrandChild Trim(ObjItem.ADsPath)
End If
Next
Set ObjGrand = Nothing
End Sub

🚀 How to Execute

To run this script correctly and avoid “Windows Script Host” popup boxes for every line, you must use the command-line engine (CScript).

Example Command: CScript /NoLogo ListAllOUs_ParentChild.vbs

Output Preview:

Parent OU: Sales

Child OU: North_Region

Child OU: South_Region

>> Child OU: Retail_Stores

#ActiveDirectory #SysAdmin #WindowsServer #Automation #VBScript #ITAdmin #LazyAdmin #LDAP #DirectoryServices #InfrastructureAsCode #Scripting #ADUC

Deep Audit: Listing Nested Active Directory Group Members via VBScript | Lazy Admin Blog

Posted on Updated on

Have you ever looked at a “Domain Admins” group and thought it looked suspiciously small? The culprit is usually nesting. Standard AD queries often fail to “recurse,” meaning they show you the subgroup but not the people inside it.

This script, ListGroupMembers_IncludingNested.vbs, uses a recursive function to dive into every sub-group and extract the actual users, ensuring your security audits are 100% accurate.

The Script: How it Works

The script utilizes a Dictionary Object to keep track of groups it has already scanned. This is a critical “Lazy Admin” safety feature—it prevents the script from getting stuck in an infinite loop if two groups are members of each other.

Usage Instructions

  1. Copy the code below into Notepad.
  2. Edit the StrGroupName variable to match your target group.
  3. Save the file as ListGroupMembers.vbs.
  4. Run it from the command prompt using cscript ListGroupMembers.vbs.
VBScript
' -- Save as ListGroupMembers_IncludingNested.vbs
Option Explicit
Dim ObjRootDSE, ObjConn, ObjRS, ObjCustom
Dim StrDomainName, StrGroupName, StrSQL, StrGroupDN, StrEmptySpace
Set ObjRootDSE = GetObject("LDAP://RootDSE")
StrDomainName = Trim(ObjRootDSE.Get("DefaultNamingContext"))
' -- Edit the line below with your Group Name
StrGroupName = "YourGroupNameHere"
StrSQL = "Select ADsPath From 'LDAP://" & StrDomainName & "' Where ObjectCategory = 'Group' AND Name = '" & StrGroupName & "'"
Set ObjConn = CreateObject("ADODB.Connection")
ObjConn.Provider = "ADsDSOObject": ObjConn.Open "Active Directory Provider"
Set ObjRS = ObjConn.Execute(StrSQL)
If ObjRS.EOF Then
WScript.Echo "Group not found: " & StrGroupName
Else
StrGroupDN = Trim(ObjRS.Fields("ADsPath").Value)
Set ObjCustom = CreateObject("Scripting.Dictionary")
GetAllNestedMembers StrGroupDN, " ", ObjCustom
End If

Why VBScript in 2026?

While PowerShell is the modern standard, many legacy environments and automated scheduled tasks still rely on VBScript because it requires zero execution policy changes and runs natively on every Windows machine since Server 2000. It is the “Old Reliable” of the AD world.

Key Features of this Script

  • Recursive Discovery: It doesn’t just stop at the first layer.
  • Class Identification: Clearly marks if a member is a User, Computer, or another Group.
  • Loop Protection: Uses the Scripting.Dictionary to escape circular nesting traps.

#ActiveDirectory #WindowsServer #CyberSecurity #SysAdmin #ITAudit #VBScript #Automation #LazyAdmin #TechArchive

The Ultimate Server Audit: Deep-Dive Inventory to Excel (VBScript) | Lazy Admin Blog

Posted on Updated on

If you are facing a massive compliance audit or a data center migration, “basic” info isn’t enough. You need to know exactly what is under the hood: What roles are active? How much disk space is actually left? What random software was installed three years ago?

This VBScript is a one-stop-shop. It checks network connectivity and then scrapes WMI and the Registry to build a massive, multi-column Excel report.

What this Script Collects:

  • Hardware: Manufacturer, Model, CPU Type, and RAM (converted to GB).
  • OS Details: Version, Caption, and the exact Installation Date.
  • Storage: Total Size vs. Free Space (with a Red-Alert highlight if space is < 20%).
  • Network: DHCP status, IP, Subnet, and Gateway.
  • Software & Roles: Every Windows Server Role/Feature and every application listed in the Registry’s Uninstall key (including version and install date).

Preparation

  1. Directory: Create C:\Temp on your local machine.
  2. Input: Create a file named ServerList.txt in C:\Temp with your server names (one per line).
  3. Excel: Ensure Microsoft Excel is installed.

The Script: Server_Inventory.vbs

VBScript
' Save as Server_Inventory.vbs in C:\Temp
' lazyadminblog.com - Ultimate Inventory Script
On Error Resume Next
dtmDate = Date
strMonth = Month(Date)
strDay = Day(Date)
strYear = Right(Year(Date),2)
strFileName = "C:\Temp\ServerInventory_" & strMonth & "-" & strDay & "-" & strYear & ".xls"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
Set fso1 = CreateObject("Scripting.FileSystemObject")
Set pcfile = fso1.OpenTextFile("C:\Temp\ServerList.txt",1)
Wscript.Echo "Audit in progress... Please wait!"
'--- Setup Header Row ---
Sub SetupHeader(col, text)
objExcel.Cells(1, col).Value = text
objExcel.Cells(1, col).Font.Colorindex = 2
objExcel.Cells(1, col).Font.Bold = True
objExcel.Cells(1, col).Interior.ColorIndex = 23
objExcel.Cells(1, col).Alignment = -4108 ' Center
End Sub
SetupHeader 1, "Computer Name"
SetupHeader 2, "Manufacturer"
SetupHeader 3, "Model"
SetupHeader 4, "RAM (GB)"
SetupHeader 5, "Operating System"
SetupHeader 6, "Installed Date"
SetupHeader 7, "Processor"
SetupHeader 8, "Drive"
SetupHeader 9, "Drive Size (GB)"
SetupHeader 10, "Free Space (GB)"
SetupHeader 11, "Adapter Description"
SetupHeader 12, "DHCP Enabled"
SetupHeader 13, "IP Address"
SetupHeader 14, "Subnet"
SetupHeader 15, "Gateway"
SetupHeader 16, "Roles & Features"
SetupHeader 17, "Installed Software"
SetupHeader 18, "Install Date"
SetupHeader 19, "Version"
SetupHeader 20, "Size"
y = 2
Do While Not pcfile.AtEndOfStream
computerName = pcfile.ReadLine
Err.Clear
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & computerName & "\root\cimv2")
If Err.Number = 0 Then
' Fetch Queries
Set colSettings = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
Set colOSSettings = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
Set colProcSettings = objWMIService.ExecQuery("SELECT * FROM Win32_Processor")
Set colDiskSettings = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType=3")
Set colAdapters = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objComputer In colSettings
strManufacturer = objComputer.Manufacturer
strModel = objComputer.Model
strRAM = FormatNumber((objComputer.TotalPhysicalMemory / (1024^3)), 2)
For Each objOS In colOSSettings
strOS = objOS.Caption
OSinstDate = CDate(Mid(objOS.InstallDate,1,4)+"/"+Mid(objOS.InstallDate,5,2)+"/"+Mid(objOS.InstallDate,7,2))
For Each objProc In colProcSettings
strProc = objProc.Name
' Populate Static Info
objExcel.Cells(y, 1).Value = computerName
objExcel.Cells(y, 2).Value = strManufacturer
objExcel.Cells(y, 3).Value = strModel
objExcel.Cells(y, 4).Value = strRAM
objExcel.Cells(y, 5).Value = strOS
objExcel.Cells(y, 6).Value = OSinstDate
objExcel.Cells(y, 7).Value = strProc
' Drive Logic
a = y
For Each objDisk In colDiskSettings
objExcel.Cells(a, 8).Value = objDisk.DeviceID
sz = objDisk.Size / (1024^3)
fr = objDisk.FreeSpace / (1024^3)
objExcel.Cells(a, 9).Value = FormatNumber(sz, 2)
objExcel.Cells(a, 10).Value = FormatNumber(fr, 2)
If fr < (sz * 0.2) Then objExcel.Cells(a, 10).Interior.ColorIndex = 3 ' Low Space Alert
a = a + 1
Next
' Network Logic
b = y
For Each objAdapter In colAdapters
objExcel.Cells(b, 11).Value = objAdapter.Description
objExcel.Cells(b, 12).Value = objAdapter.DHCPEnabled
If Not IsNull(objAdapter.IPAddress) Then objExcel.Cells(b, 13).Value = objAdapter.IPAddress(0)
If Not IsNull(objAdapter.IPSubnet) Then objExcel.Cells(b, 14).Value = objAdapter.IPSubnet(0)
If Not IsNull(objAdapter.DefaultIPGateway) Then objExcel.Cells(b, 15).Value = objAdapter.DefaultIPGateway(0)
b = b + 1
Next
' Roles & Features
x = y
Set colRoleFeatures = objWMIService.ExecQuery("Select * from Win32_ServerFeature")
If colRoleFeatures.Count > 0 Then
For Each objRole In colRoleFeatures
objExcel.Cells(x, 16).Value = objRole.Name
x = x + 1
Next
Else
objExcel.Cells(x, 16).Value = "None Found"
End If
' Software Registry Scan
s = y
Const HKLM = &H80000002
strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
Set objReg = GetObject("winmgmts://" & computerName & "/root/default:StdRegProv")
objReg.EnumKey HKLM, strKey, arrSubkeys
For Each strSubkey In arrSubkeys
objReg.GetStringValue HKLM, strKey & strSubkey, "DisplayName", strVal1
If strVal1 <> "" Then
objExcel.Cells(s, 17).Value = strVal1
objReg.GetStringValue HKLM, strKey & strSubkey, "InstallDate", strVal2
objExcel.Cells(s, 18).Value = strVal2
objReg.GetDWORDValue HKLM, strKey & strSubkey, "VersionMajor", vMaj
objReg.GetDWORDValue HKLM, strKey & strSubkey, "VersionMinor", vMin
objExcel.Cells(s, 19).Value = vMaj & "." & vMin
s = s + 1
End If
Next
' Advance Row to next available empty spot
y = a
If b > y Then y = b
If x > y Then y = x
If s > y Then y = s
y = y + 1 ' Buffer line
Next
Next
Next
Else
objExcel.Cells(y, 1).Value = computerName
objExcel.Cells(y, 2).Value = "OFFLINE"
objExcel.Cells(y, 2).Interior.ColorIndex = 3
y = y + 1
End If
Loop
' Final Formatting
For col = 1 To 20: objExcel.Columns(col).AutoFit: Next
objExcel.ActiveWorkbook.SaveAs strFileName
Wscript.Echo "Complete! Report saved to " & strFileName

Why it’s a Game Changer

  • The “Red Flag” Feature: It automatically highlights any disk with less than 20% free space in Red. This instantly tells you which servers need urgent cleanup.
  • Software Archeology: Most scripts skip software lists because they are messy. This script pulls directly from the Uninstall registry keys, capturing even the apps that don’t show up in standard WMI queries.
  • Intelligent Row Management: Because software, roles, and disks all have different counts, the script calculates the “max row” used for each server and jumps to the next clear space for the next machine.

From Zero to Complete IP Inventory in 5 Seconds: The Multi-Host VBScript | Lazy Admin Blog

Posted on Updated on

Manually documenting IP addresses, MACs, and DNS settings is the definition of “busy work.” This VBScript automates the entire process. It reads a list of servers from a text file, queries each one via WMI, and builds a professional Excel report in real-time.

How to Use This Script

  1. Prepare the Input: Create a text file (e.g., servers.txt) and list your hostnames or IP addresses, one per line.
  2. Save the Script: Save the code below as IPAddressInventory.vbs.
  3. Run: Double-click the .vbs file. When prompted, provide the full path to your text file (e.g., C:\Scripts\servers.txt).
  4. Requirement: You must have Microsoft Excel installed on the machine where you run the script.

The VBScript Code

VBScript

' Save as IPAddressInventory.vbs
' Input: Text file with Hostnames/IPs
' Output: Excel Spreadsheet (IP_Addresses.xlsx)
On Error Resume Next
Const FOR_READING = 1
'--- File Input ---
strSrvListFile = InputBox ("Please enter the server list file path OR UNC file path" & vbCrLf & "Eg: C:\Scripts\server.txt" & vbCrLf & "Eg: \\servername\scripts\server.txt","File Input location")
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objReadFile = objFSO.OpenTextFile (strSrvListFile, FOR_READING)
'--- File Output ---
strOutput = objfso.GetParentFolderName(strSrvListFile) &"\"
'--- Error Handling ---
If Err.Number <> 0 Then
WScript.Echo "Please Enter a Valid file Name"
Err.Clear
WScript.Quit
End If
'--- Excel Object Creation ---
Set objExcel = CreateObject ("Excel.application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
Set objWorksheet = objWorkbook.Worksheets("Sheet1")
x = 1
y = 1
'--- Define Headers ---
objWorksheet.Cells (x, y).value = "S.No"
objWorksheet.Cells (x, y+1).value = "Server Name"
objWorksheet.Cells (x, y+2).value = "Description"
objWorksheet.Cells (x, y+3).value = "IP_Address"
objWorksheet.Cells (x, y+4).value = "Subnet"
objWorksheet.Cells (x, y+5).value = "MACAddress"
objWorksheet.Cells (x, y+6).value = "Gateway"
objWorksheet.Cells (x, y+7).value = "Preffered DNS"
objWorksheet.Cells (x, y+8).value = "Primary DNS"
objWorksheet.Cells (x, y+9).value = "Secondary DNS"
objWorksheet.Cells (x, y+10).value = "Additional DNS 1"
objWorksheet.Cells (x, y+11).value = "Additional DNS 2"
objWorksheet.Cells (x, y+12).value = "WINS Primary"
objWorksheet.Cells (x, y+13).value = "WINS Secondary"
objWorksheet.Cells (x, y+14).value = "DNS Suffix"
objWorksheet.Cells (x, y+15).value = "DNS Suffix Order"
objWorksheet.Cells (x, y+16).value = "Remarks"
s = 1
Do Until objReadFile.AtEndOfStream
k = 0
arrComputer = objReadFile.ReadLine
strServer = Split (arrComputer, ",")
objWorksheet.Cells (x+1, y).value = s
objWorksheet.Cells (x+1, y+1).value = strServer(k)
Set objWMIService = GetObject ("winmgmts:" & "!\\" & strServer(k) & "\root\cimv2")
'--- Query Network Information ---
If Err.Number = 0 Then
WScript.Echo strServer(k) &": Inventoring"
Set colAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objAdapter in colAdapters
objWorksheet.Cells(x+1, y+2).Value = objAdapter.Description
' IP Address Logic
If Not IsNull(objAdapter.IPAddress) Then
For i = LBound(objAdapter.IPAddress) To UBound(objAdapter.IPAddress)
If Not InStr(objAdapter.IPAddress(i),":") > "0" Then
objWorksheet.Cells(x+1, y+3).Value = objAdapter.IPAddress(i)
End If
Next
End If
' Subnet Logic
If Not IsNull(objAdapter.IPSubnet) Then
For i = LBound(objAdapter.IPSubnet) To UBound(objAdapter.IPSubnet)
If objAdapter.IPSubnet(i)<> "64" Then
objWorksheet.Cells(x+1, y+4).Value = objAdapter.IPSubnet(i)
End If
Next
End If
objWorksheet.Cells(x+1, y+5).Value = objAdapter.MACAddress
' Gateway Logic
If IsNull(objAdapter.DefaultIPGateway) Then
objWorksheet.Cells(x+1, y+6).Value = "Gateway Not Set"
Else
For i = LBound(objAdapter.DefaultIPGateway) To UBound(objAdapter.DefaultIPGateway)
objWorksheet.Cells(x+1, y+6).Value = objAdapter.DefaultIPGateway(i)
Next
End If
' DNS Logic
If IsNull(objAdapter.DNSServerSearchOrder) Then
objworksheet.Cells(x+1, y+7).Value = "DNS Not Set"
Else
For i = LBound(objAdapter.DNSServerSearchOrder) To UBound(objAdapter.DNSServerSearchOrder)
objWorksheet.Cells(x+1, y+7).Value = objAdapter.DNSServerSearchOrder(i)
y = y + 1
Next
End If
y = 1
objWorksheet.Cells(x+1, y+12).Value = objAdapter.WINSPrimaryServer
objWorksheet.Cells(x+1, y+13).Value = objAdapter.WINSSecondaryServer
objWorksheet.Cells(x+1, y+14).Value = objAdapter.DNSDomain
' Suffix Logic
If IsNull(objAdapter.DNSDomainSuffixSearchOrder) Then
objworksheet.Cells(x+1, y+14).Value = "Suffix Order NA"
Else
For i = LBound(objAdapter.DNSDomainSuffixSearchOrder) To UBound(objAdapter.DNSDomainSuffixSearchOrder)
objWorksheet.Cells(x+1, y+15).Value = objAdapter.DNSDomainSuffixSearchOrder(i)
x = x + 1
Next
x = x - 1
End If
x = x + 1
WScript.Echo strServer(k) &": Completed"
Next
Else
' Error Handling for Offline Servers
objWorksheet.Cells(x+1, y+16).Value = Err.Number & "_" & Err.Description
WScript.Echo strServer(k) &": "& Err.Description
Err.Clear
x = x + 1
End If
s = s + 1
Loop
'--- Formatting and Saving ---
Set objRange = objWorksheet.UsedRange
objRange.EntireColumn.Autofit()
objExcel.ActiveWorkbook.Saveas strOutput & "IP_Addresses.xlsx"
MsgBox "Operation Completed Successfully " ,,"IP Address"

Key Features of the Script

  • Automatic Excel Formatting: It uses UsedRange.Autofit() to ensure the data is readable as soon as the file opens.
  • WMI Integration: It queries the Win32_NetworkAdapterConfiguration class directly from the remote machine.
  • Multi-Adapter Support: If a server has multiple enabled NICs, the script loops through each to capture all configurations.
  • Remark Logging: If a machine is unreachable, the error code and description are written directly into the Excel “Remarks” column so you know which servers to check manually.