Active Directory
A set of directory-based technologies included in Windows Server.
6,734 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
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
}