[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Powershell Bulk add many members to many Groups

Posted on 2014-08-08
8
Medium Priority
?
5,157 Views
Last Modified: 2014-08-15
Hi ,
anyonecan help me with this one? I'm alway getting sty like identity problem.
Import-Csv "C:\Scripts\Import Bulk Users into bulk groups\bulkgroups.csv" | 
ForEach { get-aduser $_.Accountname | add-adgroupmember $_.Group}

Open in new window






Group             Accountname
group1            user1
group77          user2
group1            user3
group2            user4
group2            user5
group3            user6
group3            user7
group3            user8
group4            user9
group5            user10
0
Comment
Question by:Mandy_
  • 6
8 Comments
 
LVL 22

Accepted Solution

by:
Joseph Moody earned 150 total points
ID: 40249445
Try this modification:

Import-Csv "C:\Scripts\Import Bulk Users into bulk groups\bulkgroups.csv" | 
ForEach ($User in Users) { 
add-adgroupmember  - identity $User.Group -member $user.accountname}

Open in new window

0
 
LVL 2

Author Comment

by:Mandy_
ID: 40249501
this i have already tried without success
0
 
LVL 14

Expert Comment

by:Brad Groux
ID: 40249530
I believe either one of the following should be of some help -

http://gallery.technet.microsoft.com/scriptcenter/PowerShell-Bulk-AD-Group-4d873f35

###########################################################  
# AUTHOR  : Marius / Hican - http://www.hican.nl - @hicannl   
# DATE    : 08-08-2012 
# EDIT    : 16-11-2012 
# CHANGES : Added functionality for duplicate OU names and 
#           changed the input file slightly because of this 
# COMMENT : This script does a bulk creation of Groups in 
#           Active Directory based on an input csv and the 
#           Active Directory Module.  
###########################################################  
Import-Module ActiveDirectory 
#Import CSV 
$csv = @() 
$csv = Import-Csv -Path "C:\Temp\bulk_input.csv" 
 
#Get Domain Base 
$searchbase = Get-ADDomain | ForEach {  $_.DistinguishedName } 
 
#Loop through all items in the CSV 
ForEach ($item In $csv) 
{ 
  #Check if the OU exists 
  $check = [ADSI]::Exists("LDAP://$($item.GroupLocation),$($searchbase)") 
   
  If ($check -eq $True) 
  { 
    Try 
    { 
      #Check if the Group already exists 
      $exists = Get-ADGroup $item.GroupName 
      Write-Host "Group $($item.GroupName) alread exists! Group creation skipped!" 
    } 
    Catch 
    { 
      #Create the group if it doesn't exist 
      $create = New-ADGroup -Name $item.GroupName -GroupScope $item.GroupType -Path ($($item.GroupLocation)+","+$($searchbase)) 
      Write-Host "Group $($item.GroupName) created!" 
    } 
  } 
  Else 
  { 
    Write-Host "Target OU can't be found! Group creation skipped!" 
  } 
}

Open in new window


... or ...

http://gallery.technet.microsoft.com/scriptcenter/Bulk-Add-Multiple-User-cea37238
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
LVL 2

Author Comment

by:Mandy_
ID: 40250279
Thats a nice script to create groups but i want to add for example  30 user to 20 different groups
0
 
LVL 2

Author Comment

by:Mandy_
ID: 40250763
Dear experts,

with this code below i'm always getting:
Add-ADGroupMember : The argument for the Parameter "Identity" cannot be checked. The Argument is NULL
Any idea what the problem could be?

Import-Module ActiveDirectory
Import-CSV c:\list.csv | %{
$usr=Get-ADUser $_."user"
$usrdg = $usr.distinguishedName
$grp=Get-ADGroup $_."group"
$grpDG = $grp.distinguishedName
Add-ADGroupMember -Identity $grpDG -Member $usrdg
} 

Open in new window

0
 
LVL 2

Author Comment

by:Mandy_
ID: 40250770
here's the csv
list.csv
0
 
LVL 2

Author Comment

by:Mandy_
ID: 40250819
I found the problem with csv it should be this form below but the script still not add all users to all groups.
Only user1 to group1 and so on.  Could anybody help me?

"user","group"
"User1","group1"
"User2","group2"
"User3","group3"
"User4","group4"

Open in new window

0
 
LVL 2

Author Comment

by:Mandy_
ID: 40250854
import-csv c:\temp\list6.csv | Foreach-Object {add-adgroupmember -Identity $_.group -Members $_.user}

Open in new window


i found this one but same effect as above
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Steps to fix error: “Couldn’t mount the database that you specified. Specified database: HU-DB; Error code: An Active Manager operation fail”
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question