-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemove-ADUserFromGroupByDisplayName.ps1
More file actions
55 lines (43 loc) · 1.93 KB
/
Copy pathRemove-ADUserFromGroupByDisplayName.ps1
File metadata and controls
55 lines (43 loc) · 1.93 KB
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
<#
.SYNOPSIS
Removes a user from an Active Directory group using their Display Name.
.DESCRIPTION
This script searches Active Directory for a user by Display Name and removes them from a specified AD group if they are a member.
It includes safety checks and does not prompt for confirmation unless the -WhatIf flag is used.
.PARAMETER DisplayName
The full display name of the user as it appears in Active Directory.
.PARAMETER GroupName
The name of the Active Directory group from which the user should be removed.
.EXAMPLE
.\Remove-UserFromADGroup.ps1 -DisplayName "John Doe" -GroupName "HR Access Group"
.NOTES
Requires:
- ActiveDirectory module
- Appropriate permissions to read user/group data and modify group membership
#>
param (
[Parameter(Mandatory = $true, HelpMessage = "Enter the display name of the user to remove")]
[string]$DisplayName,
[Parameter(Mandatory = $true, HelpMessage = "Enter the name of the group to remove the user from")]
[string]$GroupName
)
# Ensure the Active Directory module is loaded
if (-not (Get-Module -Name ActiveDirectory)) {
Import-Module ActiveDirectory -ErrorAction Stop
}
# Lookup the user in AD
$User = Get-ADUser -Filter { DisplayName -eq $DisplayName } -Properties SamAccountName
if ($User) {
$UserSam = $User.SamAccountName
Write-Output "Found user: $DisplayName (sAMAccountName: $UserSam)"
# Check if the user is in the group
$GroupMembers = Get-ADGroupMember -Identity $GroupName -ErrorAction Stop | Select-Object -ExpandProperty SamAccountName
if ($GroupMembers -contains $UserSam) {
Remove-ADGroupMember -Identity $GroupName -Members $UserSam -Confirm:$false
Write-Output "User '$DisplayName' has been removed from group '$GroupName'."
} else {
Write-Output "User '$DisplayName' is not currently a member of '$GroupName'. No action taken."
}
} else {
Write-Warning "User with Display Name '$DisplayName' not found in Active Directory."
}