Windows Tip Sheet
Who's in the Group?
List the members of a local group with this script.
Here's a reader request (keep 'em coming, by the way:
[email protected]
is the address, although if you've got a scripting question, please post at
www.ScriptingAnswers.com
instead):
How can I quickly list the members of a local group from a login
script or other script?
Like this:
Dim strGroup, objNetwork, strComputer, objGroup
Dim arrMembers, objMember
strGroup = InputBox("Group name?")
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
Set objGroup = GetObject("WinNT://" & strComputer & _
"/" & strGroup & ",group")
For Each objMember In objGroup.Members
arrMembers = Split(objMember.Parent,"/")
If UCase(arrMembers(Ubound(arrMembers))) = _
UCase(strComputer) Then
WScript.Echo objMember.Name
Else
WScript.Echo arrMembers(Ubound(arrMembers)) &
_
"\" & objMember.Name
End If
Next
Of course, it might be more useful to write the information
to a file located on a file server:
Dim strGroup, objNetwork, strComputer, objGroup
Dim arrMembers, objMember, objFSO, objTS
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTS = objFSO.OpenTextFile("filename",8)
strGroup = InputBox("Group name?")
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
objTS.WriteLine strComputer & ":"
Set objGroup = GetObject("WinNT://" & strComputer & _
"/" & strGroup & ",group")
For Each objMember In objGroup.Members
arrMembers = Split(objMember.Parent,"/")
If UCase(arrMembers(Ubound(arrMembers))) = _
UCase(strComputer) Then
objTS.WriteLine objMember.Name
Else
objTS.WriteLine arrMembers(Ubound(arrMembers))
& _
"\" & objMember.Name
End If
Next
objTS.Close
Notice where you'd change the filename on line 4 of this script. It's designed
to append if the file already exists. Note that this won't work great if every
computer is logging on at once and writing to the same file; you may want to
use the strComputer variable in the filename so
that each computer's output goes to a unique file. Enjoy!
About the Author
Don Jones is a multiple-year recipient of Microsoft’s MVP Award, and is Curriculum Director for IT Pro Content for video training company Pluralsight. Don is also a co-founder and President of PowerShell.org, a community dedicated to Microsoft’s Windows PowerShell technology. Don has more than two decades of experience in the IT industry, and specializes in the Microsoft business technology platform. He’s the author of more than 50 technology books, an accomplished IT journalist, and a sought-after speaker and instructor at conferences worldwide. Reach Don on Twitter at @concentratedDon, or on Facebook at Facebook.com/ConcentratedDon.