none
How to add user to one to many groups in Active directory using powershell RRS feed

  • Question

  • Hi  All

    I am working on a script to create a user and based on user type i would like to add that user into 2 to 3 groups . At the moment i have 3 groups e.g domain users, Developers, tester, 

    $NewUser = Read-Host "New Username"
    $firstname = Read-Host "First Name"
    $Lastname = Read-Host "Last Name"
    $NewName = "$firstname $lastname"

    $Group = "Dev"

    New-ADUser -SamAccountName $NewUser -Name $NewName -GivenName $firstname -Surname $lastname -displayname $Newname -UserPrincipalName $firstname@handmade.local -Path "CN=Users,DC=mydomain,DC=local" -AccountPassword (ConvertTo-SecureString -AsPlainText "Password" -Force)  -Enabled $true 
    Add-ADGroupMember -Identity $Group  -Members $NewUser

    I am not sure how to give user(sys admin) this option of choosing 1 or more groups while creating new user

    Friday, August 8, 2014 10:48 AM

Answers

  • This should work, it expects the key to be an integer, not a string.
    
    $h = @{1="Dev";2="Admins"}
     Write-Host "Please select:"
     $h.getEnumerator() |sort | % {
    Write-Host ([string]$_.Name +". " + $_.Value)
    } 
    Write-Host
    [string] $g = Read-Host -Prompt "Select group number or numbers, coma separated"
    
    $groups = $g -split ","
    
    foreach($group in $groups){
    [int32] $no = $group
    
    Add-ADGroupMember -Identity $h.item($no) -Members $NewUser
    }

    Monday, August 11, 2014 12:09 PM

All replies

  • Just ask for a comma separated list.


    ¯\_(ツ)_/¯

    Friday, August 8, 2014 12:04 PM
  • "Group1","Group2" |
    Add-ADGroupMember -Members `
        (Read-Host -Prompt "Enter User Name")

    or

    $Users = import-csv "C:\input.csv"

    Foreach($user in $Users){      

     add-adgroupmember -identity $user.Group -member (Get-ADUser $user.Accountname)

    }

    Friday, August 8, 2014 12:52 PM
  • "Group1","Group2" |
    Add-ADGroupMember -Members `
        (Read-Host -Prompt "Enter User Name")

    or

    $Users = import-csv "C:\input.csv"

    Foreach($user in $Users){      

     add-adgroupmember -identity $user.Group -member (Get-ADUser $user.Accountname)

    }

    User type?  Ask for groups to add "user" to.

    Just ask for a comma separated list.


    ¯\_(ツ)_/¯

    Friday, August 8, 2014 12:55 PM
  • I am getting the following error: when I run this script

    New-ADUser -SamAccountName $NewUser -Name $NewName -GivenName $FirstName -Surname $LastName -displayname $NewName -UserPrincipalName $FirstName@mydomain.local -Path "CN=Users,DC=mydomain,DC=local" -AccountPassword (ConvertTo-SecureString -AsPlainText "password" -Force) -Enabled $true $h = @{1="Dev";2="Admins"} Write-Host "Please select:" $h.getEnumerator() |sort | % { Write-Host ([string]$_.Name +". " + $_.Value) } Write-Host [string]$g = Read-Host -Prompt "Select group number or numbers, coma separated" $groups = $g -split "," foreach($group in $groups){ Add-ADGroupMember -Identity ([string]$h[$group]) -Members $NewUser

    } write-host "!!!New User"$NewUser "Created!!!" exit 0 } }

    Now getting this error as soon as i select the option for groups e.g 1 or 1,2 i get this error now i am getting this error Add-ADGroupMember : Cannot find an object with identity: '' under: 'DC=mydomain,DC=local


    • Edited by k786 Monday, August 11, 2014 11:46 AM
    Monday, August 11, 2014 11:28 AM
  • Count from zero, the array does.
    Monday, August 11, 2014 11:37 AM
  • modified this line Add-ADGroupMember -Identity ([string]$h[$group]) -Members $NewUser

    and now getting the error message Add-ADGroupMember : Cannot find an object with identity: '' under: 'DC=mydomain,DC=local

    Monday, August 11, 2014 11:48 AM
  • Here is how this issue works but the whole script has numerous logic holes.

    $g=Read-Host -Prompt "Select group number or numbers, coma separated"
    $groupnumbers = $g -split ","
    
    foreach($groupnumber in $groupnumbers){
        Add-ADGroupMember -Identity $h[$groupnumber] -Members $NewUser
    }

    You will always  get an error if someone types in a non-exisitent number or doesn't enter anything.  I \recommend taking a course inbasic programming with PowerShell.


    ¯\_(ツ)_/¯


    • Edited by jrv Monday, August 11, 2014 11:55 AM
    Monday, August 11, 2014 11:54 AM
  • Even if i select 1 i get the same error
    Monday, August 11, 2014 12:05 PM
  • This should work, it expects the key to be an integer, not a string.
    
    $h = @{1="Dev";2="Admins"}
     Write-Host "Please select:"
     $h.getEnumerator() |sort | % {
    Write-Host ([string]$_.Name +". " + $_.Value)
    } 
    Write-Host
    [string] $g = Read-Host -Prompt "Select group number or numbers, coma separated"
    
    $groups = $g -split ","
    
    foreach($group in $groups){
    [int32] $no = $group
    
    Add-ADGroupMember -Identity $h.item($no) -Members $NewUser
    }

    Monday, August 11, 2014 12:09 PM
  • Great!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Thats done, Thank You soooooooooo much 
    Monday, August 11, 2014 12:17 PM