Directory Services

Listing Users

The simplest way to list users is to generate is a list of all users in a given domain or on a particular computer. The following code example can be used to list the users on the computer localmachine.

Dim myComputer
Dim member

On Error Resume Next
Set myComputer = GetObject("WinNT://localmachine,computer")
If Err.Number<>0 Then
	WScript.Echo("An error has occurred." & Err.Number)
	Exit Sub
End If

myComputer.Filter = Array("user")
For Each member In myComputer
	WScript.Echo member.Name
Next

Be aware of the Filter property of the computer object that is used to constrain the enumeration of myComputer to return only users.

To list the users that have access to a server, bind to the Users object on a particular computer. The Users object exists on every computer, and it contains a list of users that must log onto the local domain to access that computer. The global user group Domain Users is automatically added to the users in the Users object.

The following code example binds to the Users object on a computer and lists the users contained in the object.

Dim myUsers
Dim member

On Error Resume Next
Set myUsers = GetObject("WinNT://mymachine/users")
If Err.Number<>0 Then
	WScript.Echo("An error has occurred." & Err.Number)
	Exit Sub
End If

For Each member In myUsers.Members
	WScript.Echo member.Class & ": " & member.Name
Next

Be aware of the Class property of each user returned from the Users object. The users list returned may also contain groups, so the Class property is useful for differentiating which names returned belong to users and which belong to groups, because the Filter property does not exist for the Users object.