powershell script remove user from a secuurity group member

hello

i need a script to check a csv file named user.csv in this fomr:
user1
user2
user3

and remove this user from a group named groupsecurity1 (remove the membership of user from this group)

thanks you
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.

SubsunCommented:
Try..
Import-Csv User.csv | % {Remove-ADgroupmember groupsecurity1 -Member $_.Name -Confirm:$false}

Open in new window

Csv format..
Name
user1
user2
user3

Open in new window


Or try with Quest AD PS..
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28057519.html
0
cawasakiAuthor Commented:
ok i try

and a command to export the list of user member of the group plz?

thanks
0
cawasakiAuthor Commented:
ok the script work, i just need now to export user member before i delete them :)
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

SubsunCommented:
Try..
Import-Csv User.csv | % {
$User = $_.Name
Get-adprincipalgroupmembership $User | Select @{N="User";E={$User}},@{N="Group";E={$_.Name}}
	Try {
	Remove-ADgroupmember groupsecurity1 -Member $User -Confirm:$false -EA STOP
	Write-Host "Removed $User from groupsecurity1"
	}
	Catch {
	Write-Host "Error in removing $User from groupsecurity1"
	}
} | Export-Csv C:\backup.csv

Open in new window

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
cawasakiAuthor Commented:
its ok for script to export i have get it.

for your script:

Import-Csv User.csv | % {Remove-ADgroupmember groupsecurity1 -Member $_.Name -Confirm:$false}                                         

Open in new window


its possible to get it not get error when an account is not member of group and the script try to remove it?

or just say user1 is already not member of group

thanks
0
SubsunCommented:
You can use the same script which I posted.. If you want to include the specific error message in Write-host then change line 9 to..
Write-Host "Error in removing $User from groupsecurity1 - Error $($_.Exception.Message)"

Open in new window

or to
Write-Host $_.Exception.Message

Open in new window

0
cawasakiAuthor Commented:
thank you
0
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.