Wednesday, December 21, 2005

Export local group to text file

How many ways can you think of to export the members of a local group to a text file? The really simple method is running net localgroup 'group name' > textfile.txt locally, but this adds extra descriptive info that needs to be cleaned up later. You may also encounter environments where the net command is not accessible.

Here's my version with fun little interactive input and message boxes. Easy to use, works remotely, and builds the list with names only:

'retrieve local group membership

Option Explicit

Dim intCount, strGroup, strDomain, oGroup, oMember
Dim fso, myFile, strOutput

strGroup = InputBox("Group to query?")
strDomain = InputBox("Domain or machine that hosts the group?")
strOutput = InputBox("Enter the output file name:")

set oGroup = GetObject("WinNT://" & strDomain & "/" & strGroup & ",group")

Set fso = CreateObject("Scripting.FileSystemObject")
Set myFile = fso.CreateTextFile(strOutput, True)

For Each oMember in oGroup.Members
intCount = intCount + 1


msgbox "Group: " & strGroup & " hosted by: " & strDomain & " has " & intCount & " members" & (Chr(13) & Chr(10)) & "See file " & strOutput

'end of script

The last line of the script starts with msgbox and ends with strOutput, so when copying it remember to remove the carrage return / wordwrap. Same goes for the set oGroup line. Save it in notepad as yournamehere.vbs and then execute it.


  1. Anonymous2:37 PM

    Thanks this was helpful!

  2. Anonymous12:40 PM


  3. Anonymous1:51 PM

    Great tip on the net localgroup! That was something many others really have missed. Cheers!

  4. Anonymous4:58 AM

    Fantastic. I wanted to copy members of Loacl Groups to Active Directory doamin local groups. This script meant that I could copy and past members to the DL groups. Many Many Thanks

  5. Anonymous7:04 AM

    It was a great help

  6. Anonymous9:55 AM

    Awesome...really helpful...saved lot of my time...

  7. Anonymous7:22 AM

    Hi there great work !!!
    If want to know how to list groups from user ?
    Witch string i must change ?

    Many Tks