none
How to add multiple users in to multiple groups in a new users script? RRS feed

  • Question

  • I have a script which is making active directory users, and it's working great.

    Here a thing, i need that these users will add them self after the creation to 

    some groups.

    So i've figured out that thre is a cmdle "Add-ADPrincipalGroupMembership"

    but I don't know how to combine this CmdLet into my script ( i'm on PowerShell abit more the a month)

    i've tried to use another foreach statement but it didn't worked

    Here is the Code:

            cls
        #get the csv file
        $filepath = import-csv "C:\users.csv"
        
        #set the variable for the uers
        $newusers = $filepath
        
        #set Passwords for new users 
        
        $securepassword = ConvertTo-SecureString "blahblah" -AsPlainText -Force
        
        #start the loop 
        
        foreach ($user in $newusers) {
        
        
        #get user information
        
        $firstname = $user.'First Name'.Trim()
        $lastname = $user.'Last Name'.Trim()
        $loginname= $user.SamAccountName
        $UsrPrincipalName = $user.UserPrincipalName
        $jobtitle = $user.'Job Title'
        $Department= $user.Department
        $Description = $user.Description
        $OuPath= $user.Path
        $LoginScript=$user.ScriptPath
        $displayname= $user.DisplayName
        
        #creat the users in active directory
        
        $newcreatedusers = New-ADUser -PassThru -Name "$firstname $lastname" -GivenName $firstname `
         `
         -Surname $lastname  -UserPrincipalName $UsrPrincipalName `
         `
         -SamAccountName $loginname -Path $OuPath -ScriptPath $LoginScript  `
         `
         -AccountPassword $securepassword -ChangePasswordAtLogon $false  `
         `
         -Department $Department -DisplayName $displayname `
         `
         -Description $Description -Title $jobtitle  -Enabled $true  
        
        #starting a loop for adding the users to the groups
         
        Write-Host "`n"
        Write-Host "The account for $firstname $lastname created in $OuPath successfully"
        
        }
        
        
        $filepath = $Adgroups
        
        foreach ($group in $Adgroups){
         
         $adgroup = $group.Groups.splite(',')
        
        Add-ADPrincipalGroupMembership -Identity $group.Groups -members $SamAccountName
        
        }

    Here is a screenshot of the CSV file:

    • Moved by jrv Monday, March 26, 2018 6:31 AM abandoned
    Sunday, March 25, 2018 5:46 AM

All replies

  • You can only add users to one group at a time.  Loop the groups and add the users to each group in the loop.


    \_(ツ)_/

    Sunday, March 25, 2018 6:52 AM
  • You can only add users to one group at a time.  Loop the groups and add the users to each group in the loop.


    \_(ツ)_/

    and if i use "Add-ADPrincipalGroupMembership" ? i cant add users to multiple groups at the same time?
    Sunday, March 25, 2018 7:30 AM
  • Yes.  You can add a single user to multiple groups.

    When in doubt always read the help.

    https://technet.microsoft.com/de-de/library/hh852312(v=wps.630).aspx


    \_(ツ)_/

    Sunday, March 25, 2018 7:32 AM
  • Yes.  You can add a single user to multiple groups.

    When in doubt always read the help.

    https://technet.microsoft.com/de-de/library/hh852312(v=wps.630).aspx


    \_(ツ)_/

    i did read the help, but i don't understand it so well :)

    the thing is that i don't know how to combine it in my script..
    how can i make the script and loop through the just created users

    Sunday, March 25, 2018 8:57 AM
  • That is because you haven't learned PowerShell.

    Start here: Learn PowerShell  


    \_(ツ)_/

    Sunday, March 25, 2018 9:02 AM
  • I recommend that the first thing to learn in PowerShell is the "help" system.

    help help -full
    help about_foreach
    about_Command_Syntax

    To make reading help easy:

    help help -ShowWindow

    Add "-ShowWindow" to the end of any help command for the GUI version.

    THe tutorial will take you through all of this and more.  In a few hours you will be a beginning expert.


    \_(ツ)_/

    Sunday, March 25, 2018 9:41 AM
  • That is because you haven't learned PowerShell.

    Start here: Learn PowerShell  


    \_(ツ)_/

    My friend, i'm reading and trying to do stuff with powershell more the a month already, i've watched CBT, MVA and tons of tons videos on youtube, and some time's what's working in theory not working in the real life

    i'm registered in lots of forums and asking them questions to.

    second of all, it's a forum like all the rest and in the IT world reading stuff and watching youtube won't give the knowledge that experience will provide.

    and that's why i try and asking questions

    if all the things in the Help files and other websites were so easy to understand no body were using a forum.

    Please if you don't know how to help me (or don't wan't to), stop to patronize on me.
    and let some one else to answer my quastion.
    Sunday, March 25, 2018 9:47 AM
  • That is because you haven't learned PowerShell.

    Start here: Learn PowerShell  


    \_(ツ)_/

    My friend, i'm reading and trying to do stuff with powershell more the a month already, i've watched CBT, MVA and tons of tons videos on youtube, and some time's what's working in theory not working in the real life

    i'm registered in lots of forums and asking them questions to.

    second of all, it's a forum like all the rest and in the IT world reading stuff and watching youtube won't give the knowledge that experience will provide.

    and that's why i try and asking questions


    I've heard that before.  The link to MVA that I posted will teach you how to write a loop in the first hour.  Watch the all of the segments.  Do all of the examples and all of the exercises.

    To learn any technology you must put in the effort.  We cannot teach you how to use PowerShell and we cannot write your code for you.

    If you cannot learn then you should consider hiring a consultant or a certified tech to help you with your systems. 

    There are kids as young as seven years old who seem to be able to learn PowerShell.  Mostly it is just a desire to learn something.  If you always expect others to hand you an answer you will never learn and you will never be a computer technician.

    I am only posting this as a way to prod you into putting in some independent effort.  If you don't care to learn then ignore this and hope someone will be there to do it for you.

    If you had even read the help hints I posted you would at least have an attempt at a solution.


    \_(ツ)_/

    Sunday, March 25, 2018 9:54 AM
  • That is because you haven't learned PowerShell.

    Start here: Learn PowerShell  


    \_(ツ)_/

    My friend, i'm reading and trying to do stuff with powershell more the a month already, i've watched CBT, MVA and tons of tons videos on youtube, and some time's what's working in theory not working in the real life

    i'm registered in lots of forums and asking them questions to.

    second of all, it's a forum like all the rest and in the IT world reading stuff and watching youtube won't give the knowledge that experience will provide.

    and that's why i try and asking questions


    I've heard that before.  The link to MVA that I posted will teach you how to write a loop in the first hour.  Watch the all of the segments.  Do all of the examples and all of the exercises.

    To learn any technology you must put in the effort.  We cannot teach you how to use PowerShell and we cannot write your code for you.

    If you cannot learn then you should consider hiring a consultant or a certified tech to help you with your systems. 

    There are kids as young as seven years old who seem to be able to learn PowerShell.  Mostly it is just a desire to learn something.  If you always expect others to hand you an answer you will never learn and you will never be a computer technician.

    I am only posting this as a way to prod you into putting in some independent effort.  If you don't care to learn then ignore this and hope someone will be there to do it for you.

    If you had even read the help hints I posted you would at least have an attempt at a solution.


    \_(ツ)_/

    your post was reported as abuse, you are a shame to microsoft.

    Sunday, March 25, 2018 12:53 PM
  • No one here works for Microsoft.  This is a user/community forum.  It is not a free customer support forum.  These forums are not free consulting or script re-writing forums.  If you need someone to write a script for you then you can try posting here: Script requests. The request forum is for asking for others to write a script.

    Please read the following to understand tech forums:

    Please carefully review the following links to set your expectation for posting in  technical forums.

    This Forum is for Scripting Question Rather than script requests

    Script Gallery.

    Script Center

    Learn PowerShell  

    Script requests

    From a Bill Stewart summary of useful forum links:


    \_(ツ)_/


    • Edited by jrv Sunday, March 25, 2018 1:09 PM
    Sunday, March 25, 2018 1:08 PM