Support Forums

Adding Multiple Users to Multiple AD Groups

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
User avatar
cstewart28
Posts: 12
Joined: Mon Jan 12, 2015 1:24 pm

Adding Multiple Users to Multiple AD Groups

Postby cstewart28 » Sun Feb 14, 2016 10:41 am

I'm trying to find a solutions for adding around 1000 AD accounts into multiple (up to 25 different) AD groups with not all AD accounts being in every AD group. The source data will be a csv file with the following format:

Name adGroup1 adGroup2 adGroup3......adGroup25
adUser1 X X
adUser2 X
adUser3 X X X

I'm trying to work out some logic to search every user and where their is an "x" add them to the correct adGroup. I know how to add the user to group, just having trouble working out the search logic. I have thought of trying making each group and user on one line, such as:

adUser1, adGroup1
adUser1, adGroup3
adUser2, adGroup2
adUser3....

But this would take a very long time and I would have to figure out some Excel VBA stuff for that to work, plus we are talking about around 1000 adUser accounts.

Also I get this data from the PM so having them change their process, will not work. The process has been manual and I'm looking for ways to be more efficient.

Thanks

Chris
User avatar
jvierra
Posts: 10026
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Adding Multiple Users to Multiple AD Groups

Postby jvierra » Sun Feb 14, 2016 12:39 pm

Just use Import-Csv and enumerate the records. For each column that is checked add user to group. Use a list of if statements to add to selected groups.

This would take only about 3 lines plus one line for each group.
User avatar
cstewart28
Posts: 12
Joined: Mon Jan 12, 2015 1:24 pm

Re: Adding Multiple Users to Multiple AD Groups

Postby cstewart28 » Wed Feb 17, 2016 2:11 pm

I have been trying, but I guess I thought I could make it work, but it is not working out.
Could you please help me out.

#CSV file format
#adusers,adgroup1, adgroup2, adgroup3
#aduser1 X X
#aduser2 X
#aduser3 X X
 
$adusers = @()
$group = "sAppV-Firefox"
 
$group = Get-ADGroup "UAT-MID-Applications"
 
Import-Csv C:\tools\UAT-Testers.csv |`
ForEach-Object {
$adusers += $_.adusers
}
 
ForEach($person in $adusers){
$user = Get-ADUser -Filter {SamAccountName -like $person}
Add-ADGroupMember -Identity $group -Members $user
}


Thanks
User avatar
jvierra
Posts: 10026
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Adding Multiple Users to Multiple AD Groups

Postby jvierra » Wed Feb 17, 2016 2:25 pm

You have to enumerate all group columns and add when the column contains an X.

Import-Csv file.csv|
ForEach-Object{
$user=Get-AdUser $_.adusers
if($_.Group1){ Add-AdGroupMember Group1 -member $user
if($_.Group2){ Add-AdGroupMember Group2 -member $user
if($_.Group3){ Add-AdGroupMember Group3 -member $user
.... etc ...
}
User avatar
MikeFRobbins
Posts: 5
Joined: Sun Mar 03, 2013 12:45 pm
Contact:

Re: Adding Multiple Users to Multiple AD Groups

Postby MikeFRobbins » Thu Feb 18, 2016 3:23 pm

Consider making the additions based on group instead of user since Add-ADGroupMember allows multiple users to be added to a single group at the same time.

$Header = ((Get-Content -Path C:\tmp\test.csv -TotalCount 1) -split ',').Trim()
$Users = Import-Csv C:\tmp\test.csv
 
foreach ($Group in $Header[1..($Header.Count -1)]) {
Add-ADGroupMember -Identity $Group -Members ($Users | Where-Object $Group -eq 'X' | Select-Object -ExpandProperty $Header[0]) -WhatIf
}


Remove the "-WhatIf" after you test the code and you're ready to run it for real.
Mike F Robbins
Microsoft MVP, SAPIEN MVP
User avatar
cstewart28
Posts: 12
Joined: Mon Jan 12, 2015 1:24 pm

Re: Adding Multiple Users to Multiple AD Groups

Postby cstewart28 » Wed Mar 02, 2016 9:29 pm

Thank you however I tried and got errors,

Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an argument that is not null or empty, and then
try the command again.
At C:\Users\admin\Documents\Add Users from Security Matrix.ps1:11 char:49
+ Add-ADGroupMember -Identity $Group -Members ($Users | Where-Object $Group -e ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
User avatar
jvierra
Posts: 10026
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Adding Multiple Users to Multiple AD Groups

Postby jvierra » Wed Mar 02, 2016 10:11 pm

We have no idea which code you used. Also you need to check to be sure that the user specified exists.

Import-Csv file.csv|
ForEach-Object{
if($user=Get-AdUser $_.adusers){
if($_.Group1){ Add-AdGroupMember Group1 -member $user
if($_.Group2){ Add-AdGroupMember Group2 -member $user
if($_.Group3){ Add-AdGroupMember Group3 -member $user
#.... etc ...
}else{
Write-Host 'user not found'
}
}
User avatar
MikeFRobbins
Posts: 5
Joined: Sun Mar 03, 2013 12:45 pm
Contact:

Re: Adding Multiple Users to Multiple AD Groups

Postby MikeFRobbins » Thu Mar 03, 2016 9:42 am

Looks like you were using the previous code I posted and it appears that you're running it on a machine with PowerShell version 2. I typically write all of my code to be compatible with PowerShell version 3 or higher.

Here's a version that should work with PowerShell version 2.0:

Import-Module -Name ActiveDirectory
$Header = (Get-Content -Path C:\tmp\UserGroups.csv -TotalCount 1) -split ','
$Users = Import-Csv -Path C:\tmp\UserGroups.csv
 
foreach ($Group in $Header[1..($Header.Count -1)]) {
Add-ADGroupMember -Identity $Group -Members ($Users | Where-Object {$_.$Group -eq 'X'} | Select-Object -ExpandProperty $Header[0]) -WhatIf
}
Last edited by MikeFRobbins on Thu Mar 03, 2016 9:52 am, edited 1 time in total.
Mike F Robbins
Microsoft MVP, SAPIEN MVP
User avatar
jvierra
Posts: 10026
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Adding Multiple Users to Multiple AD Groups

Postby jvierra » Thu Mar 03, 2016 9:45 am

Mike's method will work well and save a lot of typing.
User avatar
jvierra
Posts: 10026
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Adding Multiple Users to Multiple AD Groups

Postby jvierra » Sat Mar 05, 2016 12:25 pm


Return to “Windows PowerShell”

Who is online

Users browsing this forum: No registered users and 2 guests