question

BoBgsted-9807 avatar image
0 Votes"
BoBgsted-9807 asked RichMatheisen-8856 answered

adding multiple users to multiple security groups

Hello Technet.

I'm trying to look around after scripts that can handle adding multiple users to AD security groups just by looking after an " X " in a either a CSV or excel file,
acordingly to line where the X is

but I can't seam to find anything usefull only ones that need the group name instead of the X


135720-capture.png


windows-server-powershellwindows-active-directory
capture.png (10.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

I've generalized you problem a little bit. The one you presented is limited to adding each user to only a single group. But what if you had to add each user to one or more groups? The code below will work with one or more users to be added to each group:

 [array]$ColumnsToOmit = "User names"
    
 $ColumnsToScan = @()
 $groups = @{}
 [array]$data = Import-Csv c:\junk\g.csv
    
 $ColumnsToScan =    $data[0] |
                         Get-Member -MemberType NoteProperty | 
                             Select-Object -expand name |
                                 Where-Object {$ColumnsToOmit -notcontains $_}
 ForEach ($user in $data) {
     ForEach ($ColumnName in $ColumnsToScan){
             if ($user.$ColumnName -eq 'x'){
                 [array]$groups[$ColumnName] += $user."User names"
             }
         }
     }
 $groups.GetEnumerator() |
     ForEach-Object{
 #       "Adding $($_.Value) to $($_.Name)"
         Add-ADGroupMember -Identity $_.Name -Members $_.Value
     }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.