Categories / Scripting / VBScript for Windows

Get Active Directory users using WMI

Use the script below to get a specific user object from Active Directory using WMI (Windows Management Instrumentation).

The script queries Active Directory using the LDAP protocol and WMI and prints out the first name and surname of the user if the specified user is in the directory.

Option Explicit
Dim strComputer, strUsername, objWMI, colUsers, objUser

strComputer = "."
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\directory\LDAP")

strUsername = InputBox("Please type the user name")
Set colUsers = objWMI.ExecQuery("SELECT * FROM ds_user where ds_sAMAccountName = '" & strUsername & "'")
  
If colUsers.Count > 0 Then
   For Each objUser in colUsers
      WScript.Echo "First Name: " & objUser.ds_givenName
      WScript.Echo "Last Name: " & objUser.ds_sn
   Next
Else
   WScript.Echo "No users found!"
End If

06-07-2009 by Thomas Møller Nexø
Unique visits since publich date 9218
  

Get Active Directory users using WMI

Comments (2)

 

Can we create home directory using vbscript 12-11-2009 09:25
How can we assign home directory to user is that through vbscript or .bat file
 
Thomas Møller Nexø 12-11-2009 17:29
Hi.

When you have initiated the user object like in the code above, you can use the user objects Homedirectory property to assign the users homedirectory like:

objUser.Put "homeDirectory", "\\ServerName\HomeFolders\Username"

So for the user called Dan Willis, the path to the homedirectory would be something like:

objUser.Put "homeDirectory", "\\ServerName\HomeFolders\DanWillis"
 

Comment on this article

Name
Email (receive replies)
Comment
What is: 11 + 13
 

Valid XHTML 1.0 Transitional Valid CSS! Subscribe to RSS feeds from my blog about all aspects about system administration in a Microsoft Windows server environment