-
Notifications
You must be signed in to change notification settings - Fork 1
/
SetUserGroupMembership.ps1
72 lines (57 loc) · 3.19 KB
/
SetUserGroupMembership.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#REQUIRES -Version 2.0
<#
.SYNOPSIS
Add or Remove users from groups defined in a CSV file
.DESCRIPTION
Add or Remove users from groups defined in a CSV file
The CSV file format is as follows:
UserName,Group
MyUserExample,MyADGroupExample
MyUserExample,MyADGroupExample2
MyUserExample2,MyADGroupExample
MyUserExample3,MyADGroupExample2
.PARAMETER file
Name of the CSV file specifying the users and groups (required)
.PARAMETER action
The action to perform:
add - Adds the user to the specified group (default)
remove - Remove the user for the specified group
.NOTES
File Name : SetUserGroupMembership.ps1
Author : Allan Scullion
Prerequisite : PowerShell V2, ActiveDirectory module
.EXAMPLE
.\SetUserGroupMembership.ps1 -file Test.csv -action add
.EXAMPLE
.\SetUserGroupMembership.ps1 -file Test.csv -action remove
#>
param (
[parameter(Mandatory=$true)]
[alias("f")]
[string]$file = $(throw "-file is required."),
[parameter(Mandatory=$false)]
[alias("a")]
[string]$action = "add"
)
if (Test-Path $file) {
$dataSource = Import-Csv $file
foreach($dataRecord in $dataSource)
{
$user = $dataRecord.UserName
$group = $dataRecord.Group
if ($action -eq "remove")
{
Write-Host "Removing $user from $group"
Remove-ADGroupMember -Confirm:$false -Identity $group -Member $user
}
else
{
Write-Host "Adding $user to $group"
Add-ADGroupMember -Confirm:$false -Identity $group -Member $user
}
}
}
else
{
Write-Host "ERROR:$file does not exist."
}