-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAdd-MemberstoGroups.ps1
26 lines (23 loc) · 1.31 KB
/
Add-MemberstoGroups.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# from a CSV file, this will add members to specific groups, one member at a time
$CSVPath = ".\group-membership.csv"
$GroupNameHeader = "group"
$AddtoGroupHeader = "addto"
Import-Csv $CSVPath | ForEach-Object {
if (! (Get-ADGroup -Identity $($_.$GroupNameHeader))) {
Write-Host -ForegroundColor Red -BackgroundColor Black "ERROR : Group "$($_.$GroupNameHeader)" doesn't exist"
Write-Host "Skipping adding $($_.$GroupNameHeader) to $($_.$AddtoGroupHeader)"
return
}
if (! (Get-ADGroup -Identity $($_.$AddtoGroupHeader))) {
Write-Host -ForegroundColor Red -BackgroundColor Black "ERROR : Group "$($_.$AddtoGroupHeader)" doesn't exist"
Write-Host "Skipping adding $($_.$GroupNameHeader) to $($_.$AddtoGroupHeader)"
return
}
if ((Get-ADGroupMember -Identity $($_.$AddtoGroupHeader) | Select -ExpandProperty Name) -contains $($_.$GroupNameHeader) ) {
Write-Host -ForegroundColor White -BackgroundColor Yellow "WARNING : Group "$($_.$GroupNameHeader)" is already a member of "$($_.$AddtoGroupHeader)
Write-Host "Skipping adding $($_.$GroupNameHeader) to $($_.$AddtoGroupHeader)"
return
}
Write-Host "Adding $($_.$GroupNameHeader) to $($_.$AddtoGroupHeader)"
Add-ADGroupMember -Identity $($_.$AddtoGroupHeader) -Members $($_.$GroupNameHeader)
}