Announcement

Collapse
No announcement yet.

Adding usernames from excel to groups in AD

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adding usernames from excel to groups in AD

    I have a excel sheet with about 100 usernames.

    I have a group in AD that i want to add all this users to.

    All the users already exist in ad, I just want to add them to a specific group.

    I'm not very good witch, VB or VBS so if someone would have the chance to give me a script that will do this, and tell me what i need to change on it (like where to i put the path to the file, write the group, write the ou and so on. I would be very grateful.

    Conda

  • #2
    Re: Adding usernames from excel to groups in AD

    Without knowing more about your setup, and the fact that you admit you're not comfortable with scripting tools, what's the effort in using the graphical environment already available through Users & Computers?

    The DSMOD command-line utility with Server 2003 will do exactly what you want, but it's a bit chancy to write a script for you without any more info to go on.

    Comment


    • #3
      Re: Adding usernames from excel to groups in AD

      It's because i have e excel sheet with 100 users that ar going to be in a group. Then next time, 70 other users in that group. Would take up pretty much time to add every user every time.

      Much easyer to do with a script. But i found the solution elsewhere.

      rem set /a n=0
      @echo off
      for /f %%i in (filepath\filename.txt) do net group ADGROUP %%i /add

      Just puts the path for the file where it stands, and write the group i want to add them in "ADGROUP" and all the users is added to the group.

      Comment


      • #4
        Re: Adding usernames from excel to groups in AD

        Originally posted by Conda View Post
        Originally posted by Rickles View Post
        what's the effort in using the graphical environment already available through Users & Computers? ..
        It's because i have e excel sheet with 100 users that ar going to be in a group. Then next time, 70 other users in that group. Would take up pretty much time to add every user every time.

        Much easyer to do with a script. But i found the solution elsewhere.

        rem set /a n=0
        @echo off
        for /f %%i in (filepath\filename.txt) do net group ADGROUP %%i /add

        Just puts the path for the file where it stands, and write the group i want to add them in "ADGROUP" and all the users is added to the group.

        Glad you got it sorted. Thanks for sharing!

        One short note, If there would be any spaces in the path or, any embedded spaces in the name of the group, consider these little changes:
        Code:
        @echo off
        For /f "usebackq" %%! in ("c:\file path\filename.txt") do (
           net group "Name of Group" %%! /ADD /DOMAIN)


        However..
        I would rather use dsmod myself, like Rickles also suggested:
        (Batch)
        Code:
        :: This batch is using 'DSMOD GROUP ...'
        :: for adding members to the group from a users-list
        
        @echo off & color 6A
        title Add members to Group
        
        set "strGoup=Name of Group"
        Set "inputFile=c:\file path\filename.txt"
        
        For /f "delims=" %%* in (
           'dsquery group -samid "%strGoup%"') Do Set "dnGroup=%%*"
        echo Adding new members to: %dnGroup%
        echo\--- Please wait..., --- 
                
        For /f "usebackq" %%! in ("%inputFile%") Do (
          For /f "delims=" %%* in ('dsquery user -samid %%!') Do (
          echo\&echo\%%~*
          >nul 2>&1 dsmod group %dnGroup% -addmbr %%*)
        ) 
        
        echo\& echo Done!
        
        pause >nul
        just FYI:
        Of course the file that you are now using "filename.txt" is not an excel file - while the title of the thread stated "usernames from excel" and in your posts you mensioned that you have an excel sheet not a txt file.

        Although I know that it is very easy to export the worksheet from an excel format to a txt-file or a csv-file. And also that a txt-format file is simpler to read by a script than having the script read from a worksheet - anyway, here is a sample how you can read usernames from a xls-file,
        (VBScript)
        Code:
        '-----------------------------------------------------------------------
        '::  Get the users from a list (xls-file) and make them
        '::  a member of a certain domain group.
        '::
        '::  Run this vbscript as a member of the domain admins group
        '::  from a computer where MS-Excel is installed on.
        '-----------------------------------------------------------------------
        
        strInputFile = "c:\file path\filename.XLS"
        strGroupName = "Name of Group"
        strDomain = "DomainShortName"
        
        ' Bind to the domain group object
        Dim objGroup: Set objGroup = GetObject("WinNT://" _
               & strDomain & "/" & strGroupName & ",group")
        
        ' Read accountnames from xls-file
        Set objExcel = CreateObject("Excel.Application")
        objExcel.DisplayAlerts = False
        
        On Error Resume Next
        Set objWorkbook = objExcel.Workbooks.Open(strInputFile)
        
        ' number or name of the WorkSHEET that should be activated:
        rdWS = 1
        objworkbook.worksheets(rdWS).Activate
        
        ' get values from just the following column:
        rdColumn = 1
        
        ' reading down starting from row:
        intRow = 1
        
        Do Until iCnt >100 '(close xls when the last 100 values where empty)
           strVal = Trim(objExcel.Cells(intRow, rdColumn).Value)
           If Len(strVal) > 0 Then
             call AddToGroup(strDomain & "/" & strVal)
             iCnt = 0
           Else iCnt = iCnt+1 : End If
           intRow = intRow + 1
        Loop
        
        objExcel.Quit
        Wscript.Quit 0
        
        Sub AddToGroup(accountname)
           'Make the account member of the group
           On Error Resume Next
           With objGroup
             .Add ("WinNT://" & accountname)
           End With
           If Err.number <>0 Then err.clear
        End Sub

        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Adding usernames from excel to groups in AD

          Yes of course it is not a excel file anymore, forgot to mension that =)

          And thanks for the scripts posted, always good to have "one less step" when you have busy work schedule.

          Comment

          Working...
          X