Powershell: Remove multiple users from a single AD group
Remove multiple users from a single AD group
https://www.powershellbros.com/remove-user-from-specifc-ad-groups-using-powershell/
Start-Transcript -Path C:\Temp\Remove-ADUsers.log -Append
#Taking AD group member detail and will send them by Mail.
#Getting AD group member detail and save in CSV format.
(Get-ADGroup "AD-Group-Name" -properties members).members |
Get-ADUser -properties displayName | Select-Object displayName,SamAccountName,emailaddress | export-CSV c:\users\administrator\AD-Group-Name (or File-Name).csv
#Import the user list
$Users = Import-Csv "C:\Temp\Users.csv"
#Update the AD group name which is need to be removed for listed users.
$Group = “AD-Group-Name”
$Report = @()
foreach ($User in $Users) {
$UPN = $User.UserPrincipalName
$ADUser = Get-ADUser -Filter "UserPrincipalName -eq '$UPN'" | Select-Object SamAccountName
$ExistingGroups = Get-ADPrincipalGroupMembership $ADUser.SamAccountName | Select-Object Name
#if ($ExistingGroups.Name -eq $Group) {
Remove-ADGroupMember -Identity $Group -Members $ADUser.SamAccountName -Confirm:$false -WhatIf
$From = "pugazh@pugazh.tech"
$To = "pugazh@pugazh.tech"
$Subject = "Here's the Email Subject"
$Body = "This is what I want to say"
$SMTPServer = "smtp.hostinger.in"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential (Get-Credential) –DeliveryNotificationOption OnSuccess
Stop-transcript
Comments
Post a Comment