locked
Export Users, Groups and LocalGroup Membership RRS feed

  • Question

  • Hello,

    I have a Windows Server 2008R2 WITHOUT Active Directory and I want export all local users, groups and localgroup membership into a text or csv file (which could be in different file).

    I have created a batch file which exports them with the "net" command.

    rem export user

    net user > C:\windows_user_accounts_%mydate%\user.txt

    rem export group net localgroup > C:\windows_user_accounts_%mydate%\group.txt

    rem export localgroup membership for /F "skip=5 delims=*" %%u in (C:\group.txt) do net localgroup "%%u" >> C:\localgroup.txt

    It works except the users and the localgroup membership. The textfile of user is in two columns and for import it, my script uses one column.

    In addition, the textfile of localgroup membership, I have many useless line and I can't ordinat to process it.

    here, my import script :

    for /F "skip=4 delims="" %%u in (C:\windows_user_accounts_%mydate%\user.txt) do net user "%%u" /logonpasswordchg:yes /add
    
    for /F "skip=5 delims=*" %%v in (C:\windows_user_accounts_%mydate%\group.txt) do net localgroup "%%v" /add

    I havn't did the script for localgroup member beceause I don't understand how I can do it.

    Can you help me, please ?

    Thank you.


    Tuesday, July 17, 2012 8:25 AM

Answers

  • I cannot tell exactly what you want (in what format), but command line utilities, like net user and net localgroup, have limited capabilities. You may not be able to get output in the format you want. I would suggest using VBScript. For example, the following documents all local users and the groups they are member of, and also all local groups and their memberships:

    Option Explicit

    Dim objNetwork, strComputer, objComputer, objUser, objGroup, objMember

    ' Retrieve local computer name.
    ' This can also be hard coded if the computer is remote.
    Set objNetwork = CreateObject("Wscript.Network")
    strComputer = objNetwork.ComputerName

    Set objComputer = GetObject("WinNT://" & strComputer)

    ' Enumerate all local users and their group memberships.
    objComputer.Filter = Array("user")
    For Each objUser in objComputer
        Wscript.Echo "User: " & objUser.Name
        For Each objGroup In objUser.Groups
            Wscript.Echo "-- Member of: " & objGroup.Name
        Next
    Next

    ' Enumerate all local groups and their membership.
    objComputer.Filter = Array("group")
    For Each objGroup In objComputer
        Wscript.Echo "Group: " & objGroup.Name
        For Each objMember In objGroup.Members
            Wscript.Echo "-- Member: " & objMember.Name
        Next
    Next

    -----

    You can run this at a command prompt using the cscript host program, so the output can be redirected to a text file. For example, if the VBScript code is saved in the file LocalObjects.vbs, the following (at a command prompt) will create the file report.txt with the output:

    cscript //nologo LocalObjects.vbs > report.txt

    -----

    This assumes you are in the folder where the file LocalObjects.vbs is saved. Otherwise, you must specify the full path to the file.


    Richard Mueller - MVP Directory Services

    • Proposed as answer by Richard MuellerMVP Thursday, July 26, 2012 1:58 AM
    • Marked as answer by IamMred Wednesday, August 8, 2012 5:52 PM
    Monday, July 23, 2012 4:25 PM

All replies

  • Nobody ?
    Wednesday, July 18, 2012 7:24 AM
  • Wednesday, July 18, 2012 7:46 AM
  • I cannot tell exactly what you want (in what format), but command line utilities, like net user and net localgroup, have limited capabilities. You may not be able to get output in the format you want. I would suggest using VBScript. For example, the following documents all local users and the groups they are member of, and also all local groups and their memberships:

    Option Explicit

    Dim objNetwork, strComputer, objComputer, objUser, objGroup, objMember

    ' Retrieve local computer name.
    ' This can also be hard coded if the computer is remote.
    Set objNetwork = CreateObject("Wscript.Network")
    strComputer = objNetwork.ComputerName

    Set objComputer = GetObject("WinNT://" & strComputer)

    ' Enumerate all local users and their group memberships.
    objComputer.Filter = Array("user")
    For Each objUser in objComputer
        Wscript.Echo "User: " & objUser.Name
        For Each objGroup In objUser.Groups
            Wscript.Echo "-- Member of: " & objGroup.Name
        Next
    Next

    ' Enumerate all local groups and their membership.
    objComputer.Filter = Array("group")
    For Each objGroup In objComputer
        Wscript.Echo "Group: " & objGroup.Name
        For Each objMember In objGroup.Members
            Wscript.Echo "-- Member: " & objMember.Name
        Next
    Next

    -----

    You can run this at a command prompt using the cscript host program, so the output can be redirected to a text file. For example, if the VBScript code is saved in the file LocalObjects.vbs, the following (at a command prompt) will create the file report.txt with the output:

    cscript //nologo LocalObjects.vbs > report.txt

    -----

    This assumes you are in the folder where the file LocalObjects.vbs is saved. Otherwise, you must specify the full path to the file.


    Richard Mueller - MVP Directory Services

    • Proposed as answer by Richard MuellerMVP Thursday, July 26, 2012 1:58 AM
    • Marked as answer by IamMred Wednesday, August 8, 2012 5:52 PM
    Monday, July 23, 2012 4:25 PM
  • Would anyone be able to amend Richards script to so instead of referring to the local computer, it references a text file with multiple hostnames (or IP addresses). The output would then show the group membership for multiple computers? Thanks.

    Tuesday, January 27, 2015 2:37 PM
  • Would anyone be able to amend Richards script to so instead of referring to the local computer, it references a text file with multiple hostnames (or IP addresses). The output would then show the group membership for multiple computers? Thanks.


    No.  We will allow you to modify it.   Start a new question when you get stuck.

    ¯\_(ツ)_/¯

    Tuesday, January 27, 2015 2:50 PM