-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_groups
More file actions
31 lines (26 loc) · 846 Bytes
/
Copy pathadd_groups
File metadata and controls
31 lines (26 loc) · 846 Bytes
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
# Hardcoded group names
$groupNames = @("Group1", "Group2", "Group3") # Add your group names here
# Function to add a user to a group
function Add-UserToGroup {
param(
[string]$userName,
[string]$groupName
)
try {
# Check if the group exists
$group = Get-ADGroup -Identity $groupName -ErrorAction Stop
# Add the user to the group
Add-ADGroupMember -Identity $group -Members $userName
Write-Host "User $userName has been added to group $groupName."
} catch {
Write-Host "Error: $_"
}
}
# Ask for the sAMAccountName
$sAMAccountName = Read-Host -Prompt "Enter the sAMAccountName"
# Import the Active Directory module
Import-Module ActiveDirectory
# Main logic
foreach ($group in $groupNames) {
Add-UserToGroup -userName $sAMAccountName -groupName $group
}