modify powershell script adding group member

hello,

i have this powershell script to add group to other group :

Import-Module ActiveDirectory
Import-Csv "member.csv" | %{
$Group = $_.Group
	$_.memberof -split "," | % {
		Add-ADGroupMember $_ $Group
	}
}

Open in new window


the csv file member.csv is like this:
Group,MemberOf
group1,"group2"
group3,"group4"
...

what i need to modify is before add group1 to group 2  and before add groupe 3 to group 4 is to remove all member of group 3 and group 4.

thanks for help
cawasakiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael PfisterCommented:
Not sure from your description which group you want to clear of its members. Basically cleaing all members of a group works like this:

Get-ADGroupMember "grouptoclear" | ForEach-Object {Remove-ADGroupMember "grouptoclear" $_ -Confirm:$false}

Open in new window

HTH
0
cawasakiAuthor Commented:
hello
how i can use this with my script?

thanks
0
Michael PfisterCommented:
I'm not sure if I understand your requirements right, so use with care:
Import-Module ActiveDirectory
Import-Csv "member.csv" | %{
$Group = $_.Group
Get-ADGroupMember $Group | ForEach-Object {Remove-ADGroupMember $Group $_ -Confirm:$false}
	$_.memberof -split "," | % {
		Add-ADGroupMember $_ $Group
	}
}

Open in new window

This will clear group1 and group3 before importing group2 and group4 to the correspondig group
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shaun VermaakTechnical Specialist/DeveloperCommented:
Why not just clear the members using clear?
Set-ADGroup "Some Group" -Clear member

Open in new window

1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.