Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

remove user from list of groups

Posted on 2016-10-27
2
Medium Priority
?
41 Views
Last Modified: 2016-10-27
I am looking to remove a user from a list of groups. He may be part of a few of them. But if the user is not part of one of the groups the script seems to stop.  I would like to attempt to remove the user from all of the groups even if the user is not part of them.  

$script:groups = @("group1", "group2","group3")
foreach ($adgroups in $groups) { remove-adgroupmember $adgroups –members $a -confirm:$false }

If it makes it any easier all of the groups are part of a parent group. So group2 and group3 are part of group1.  I dont know if there is a way to just remove a user from the parent group and all sub groups.
0
Comment
Question by:Roccat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 41862457
Just to "silently ignore" errors is one way:
$script:groups = @("group1", "group2","group3")
foreach ($adgroups in $groups) { remove-adgroupmember $adgroups –members $a -confirm:$false -EA SilentlyContinue } 

Open in new window

The more "polite" way is to get the groups the user is a member of, then  check against the removal list, and run only if necessary.
$script:groups = @("group1", "group2","group3")
(get-aduser $a -property MemberOf).MemberOf |
  % { $_.Split(',')[0] -replace 'CN='} |
  ? { $groups -contains $_ } |
  % { Remove-ADGroupMember $_ -Members $a -Confirm:$false }

Open in new window

2
 

Author Closing Comment

by:Roccat
ID: 41862732
Thank you!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question