Powershell-Script to remove all group memberships from list of aliases
Hello,
As per a similar thread about "Powershell-Script to remove all group memberships for one user" I use the below shell command:
$DGs= Get-DistributionGroup | where { (Get-DistributionGroupMember $_ | foreach {$_.SamAccountName}) -contains "GRAIG"}
foreach( $dg in $DGs){
Remove-DistributionGroupMember -Confirm:$false $dg -Member GRAIG
}
However I have some aliases to deactivate in a csv file that look like that:
users.csv:
GRAIG
JOHN
SARAH
I do not know how to combine the get-content C:\users.csv with the above script.
Can you help me to remove all the users from the csv file from all group membership?
Thanks,
Graig
August 20th, 2012 6:29am
Hi,
Try this
>import-csv "c:users.csv" | Remove-DistributionGroupMember -Identity "DG Name" -Member $_.Member
In the csv file member should have the username of the users.
I hope this help.
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2012 7:39am
Hi,
Thank you for your Reply. However I would need to specify the Distribution groups which is not what I am looking for.
I would like the member in the csv file to be removed from the DG there are belonging to.
Graig
August 20th, 2012 9:03am
Hi
The most simple way is do another Foreach, please try
-------------------------------------------
$members = get-content C:\users.csv
Foreach($member in $members){
$DGs= Get-DistributionGroup
| where {
(Get-DistributionGroupMember $_
| foreach {$_.SamAccountName})
-contains $member}
foreach( $dg in $DGs){
Remove-DistributionGroupMember
-Confirm:$false $dg -Member $member
}
}
------------------------------
CheersZi Feng
TechNet Community Support
Free Windows Admin Tool Kit Click here and download it now
August 21st, 2012 4:43am
Many Many Thanks ! Exactly what I was expected :)
August 21st, 2012 6:00am