0
votes

I am trying to make a script to add users to the AD groups based on header, the CSV looks like this:

Group1  Group2  Group3
UserA   UserC   UserA
UserB           UserC

The closest script I find is below, but this based below format, which is little different..

Group             Accountname
group1            user1
group1            user2
group1            user3
Import-Module ActiveDirectory
$list = Import-Csv ".\Bulk_Import.csv"

foreach ($user in $list) {
    Add-ADGroupMember -Identity $user.Group -Member (Get-ADUser $user.Accountname)
}

Is that possible for script to add users based on my format, and ignore error if user is already in the group?

1
SO is not a place where we rewrite scripts you found somewhere else according to your requirements. What have you tried to make this work? What specific problem in your code do you need help with? - Ansgar Wiechers

1 Answers

0
votes
Import-Module ActiveDirectory
$list = Import-Csv ".\Bulk_Import.csv"

ForEach ($Entry in $List) {
    $Entry.PSObject.Properties | Where {$Entry.($_.Name)} | ForEach {
        Add-ADGroupMember -Identity $_.Name -Member (Get-ADUser $Entry.($_.Name))
    }
}