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
'
'from webmaxtor.blogspot.com
'
'**********

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
myFile.Writeline oMember.name
Next

myFile.Close

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.

7 comments:

  1. Thanks this was helpful!

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

    ReplyDelete
  3. 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

    ReplyDelete
  4. It was a great help

    ReplyDelete
  5. Awesome...really helpful...saved lot of my time...
    thanks

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


    Many Tks

    ReplyDelete