SHARE
Facebook X Pinterest WhatsApp

Script to Search for a User in Active Directory

Author:Yi Helen Wang yi.wang@doj.state.or.us Platform:Windows NT Windows 2000Type:VBScript,ADSI DESCRIPTION: This script is for an administrator who wants to search for a particular user through Active Directory of local or remote Windows 2000 servers. It also prevents duplication when new users are added into Active Directory. Scroll down to view the script. SCRIPT: ‘************************************************************************************** ‘Created:04/08/2002 ‘Author: […]

Sep 26, 2003
Datamation content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Author:Yi Helen Wang yi.wang@doj.state.or.us
Platform:Windows NT

Windows 2000
Type:VBScript,ADSI

DESCRIPTION:

This script is for an administrator who wants to search for a particular user through Active Directory of local or remote Windows 2000 servers. It also prevents duplication when new users are added into Active Directory.

Scroll down to view the script.


SCRIPT:

'**************************************************************************************
'Created:04/08/2002 
'Author: Yi Helen Wang
'Purpose:Searching users through an Active Directory (AD) of
'Windows 2000 to insure against duplicating when 
'a new user is added into AD
'Keywords:ADO and ADSI 2.5 or up
'Reviewed:05/13/2002
'Comment:You have to assign a permission to get Active Directory 
'  information.  This program is run locally or remotely
'***************************************************************************************
on error resume next

dim oContainer,searchpath
dim oRoot
dim oConnect, oCommand, rs
dim strConnect, strDomainCtrl, strOU
dim cntuser, newuser, strCN, strSearchUser

newuser = false
cntuser = 0

'to get LDAP namespace
set oRoot = GetObject("LDAP: //RootDSE")
call ShowError("Permission issues !")
strDomainCtrl = oRoot.Get("defaultNamingContext")

strOU = InputBox("Please entry an Organization Unit Name to search: ", "Searching","Legal")
if Len(Trim(strOU)) = 0 then
  msgbox "No information typed in!" 
else
  strConnect = "LDAP://OU=" & strOU & ", " & strDomainCtrl
  set oContainer = GetObject(strConnect)
  call ShowError("OUNameNotExist")
  
  searchpath = oContainer.ADsPath

  strSearchUser = InputBox("Please entry the user searched initial: ", "Searching", "sls")
  if Len(Trim(strSearchUser))=0  then
msgbox "No information typed in!"
  else
'Using ADO to query the Active Directory for a particule user
set oConnect = CreateObject("ADODB.Connection")
set oCommand = CreateObject("ADODB.Command")
call ShowError("ADOProblems")

'opening the connection
oConnect.Provider = "ADsDSOObject"
oConnect.Open "Active Directory Provider"
call ShowError("ADOProvider")
 'creating a command object for this connection
 Set  oCommand.ActiveConnection = oConnect

 strCN = strSearchUser
 DO WHILE newuser  True
  
  oCommand.CommandText = "SELECT samAccountName FROM '" & searchpath & "' WHERE objectClass='user' AND samAccountName = '" & strSearchUser & "'"

  'creating a recordset based on the ADO command
  set rs = oCommand.Execute
 
  'Navigating the record set
  if rs.EOF and rs.BOF then
msgbox  "New user"
newuser = True
  else
cntuser = cntuser + 1
'rename and check again till no duplication
strSearchUser = left(strCN,1) & right(strCN,1) & cntuser
msgbox  "Rename CN as " & strSearchUser
set rs = nothing
 end if

LOOP
  end if
end if

sub ShowError(strErr)
  if err  0 then

if err = 432 or err=70 then
wscript.echo "Permission issues to run the program"
wscript.quit
else
select case strErr
 case "OUNameNotExist"
  wscript.echo "OU name is not correct or not exist !"
  wscript.quit
 case "ADOProblems"
  wscript.echo "ADO problems!"
  wscript.quit
 case "ADOProvider"
  wscript.echo "ADO Provider Problems!"
  wscript.quit
end select
end if

  end if
end sub
Disclaimer: We hope that the information in these pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by me. I shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.

 

RELATED NEWS AND ANALYSIS


Huawei's AI Update: Things Are Moving Faster Than We Think
FEATURE |  By Rob Enderle,
December 04, 2020



Keeping Machine Learning Algorithms Honest in the ‘Ethics-First’ Era
ARTIFICIAL INTELLIGENCE |  By Guest Author,
November 18, 2020



Key Trends in Chatbots and RPA
FEATURE |  By Guest Author,
November 10, 2020



Top 10 AIOps Companies
FEATURE |  By Samuel Greengard,
November 05, 2020



What is Text Analysis?
ARTIFICIAL INTELLIGENCE |  By Guest Author,
November 02, 2020



How Intel's Work With Autonomous Cars Could Redefine General Purpose AI
ARTIFICIAL INTELLIGENCE |  By Rob Enderle,
October 29, 2020



Dell Technologies World:  Weaving Together Human And Machine Interaction For AI And Robotics
ARTIFICIAL INTELLIGENCE |  By Rob Enderle,
October 23, 2020



The Super Moderator, or How IBM Project Debater Could Save Social Media
FEATURE |  By Rob Enderle,
October 16, 2020



Top 10 Chatbot Platforms
FEATURE |  By Cynthia Harvey,
October 07, 2020



Finding a Career Path in AI
ARTIFICIAL INTELLIGENCE |  By Guest Author,
October 05, 2020



CIOs Discuss the Promise of AI and Data Science
FEATURE |  By Guest Author,
September 25, 2020



Microsoft Is Building An AI Product That Could Predict The Future
FEATURE |  By Rob Enderle,
September 25, 2020



Top 10 Machine Learning Companies 2020
FEATURE |  By Cynthia Harvey,
September 22, 2020



NVIDIA and ARM: Massively Changing The AI Landscape
ARTIFICIAL INTELLIGENCE |  By Rob Enderle,
September 18, 2020



Continuous Intelligence: Expert Discussion [Video and Podcast]
ARTIFICIAL INTELLIGENCE |  By James Maguire,
September 14, 2020



Artificial Intelligence: Governance and Ethics [Video]
ARTIFICIAL INTELLIGENCE |  By James Maguire,
September 13, 2020



IBM Watson At The US Open: Showcasing The Power Of A Mature Enterprise-Class AI
FEATURE |  By Rob Enderle,
September 11, 2020



Artificial Intelligence: Perception vs. Reality
FEATURE |  By James Maguire,
September 09, 2020



Anticipating The Coming Wave Of AI Enhanced PCs
FEATURE |  By Rob Enderle,
September 05, 2020



The Critical Nature Of IBM's NLP (Natural Language Processing) Effort
ARTIFICIAL INTELLIGENCE |  By Rob Enderle,
August 14, 2020




   
  SEE ALL  
  APPLICATIONS ARTICLES
   







Recommended for you...

How Network Detection & Response (NDR) Works
Sarah Hunt
Oct 1, 2025
Common Data Visualization Examples: Transform Numbers into Narratives
Liz Ticong
Sep 24, 2025
8 Best Data Analytics Tools: Gain Data-Driven Advantage
Datamation Logo

Datamation is the leading industry resource for B2B data professionals and technology buyers. Datamation's focus is on providing insight into the latest trends and innovation in AI, data security, big data, and more, along with in-depth product recommendations and comparisons. More than 1.7M users gain insight and guidance from Datamation every year.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.