Remove users from groups based on CSV file

Powershell gurus, this is a dumb question and I'm sure I'm overlooking something simple. I need to get the SamAccountNames for AD users based on an email address that is put into a CSV file, this should be their primary email address. Then I need to take that SamAccountName and use it to remove members from one group and add them to another. My CSV is formatted very simply, but for some reason it's not pulling the email address.

email
email@email.com

$email = import-csv c:\temp\users.csv 
foreach ($user in $users) {
$email = $_.email
$user = Get-ADUser -Filter {Emailaddress -eq "$email"}
$username = $user.SamAccountName
Remove-ADGroupMember -Identity group1 -Members $username -Confirm:$false
Add-ADGroupMember -Identity group2 -Members $username 
}

Open in new window

LVL 1
vpetersenAsked:
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.

RobSampsonCommented:
Try this:
$user = Get-ADUser -Filter {mail -eq "$email"}

Rob.
0
vpetersenAuthor Commented:
Sorry should have clarified, I'm trying to write-host $email to make sure that it's pulling up just the email address out of the CSV, doesn't seem to be working. Then I need to process the rest of it. Not sure what I'm missing, probably something stupid.

$email = import-csv c:\temp\users.csv 
foreach ($user in $users) {
$email = $_.email
write-host $email }

Open in new window

0
RobSampsonCommented:
Actually, there are a couple of issues there.

The ForEach loop was never being used because you assign the CSV to $email but then loop through $users.

Regards,

Rob.

$users = import-csv c:\temp\users.csv 
foreach ($user in $users) {
    $email = $user.email
    $user = Get-ADUser -Filter {mail -eq "$email"}
    $username = $user.SamAccountName
    Remove-ADGroupMember -Identity group1 -Members $username -Confirm:$false
    Add-ADGroupMember -Identity group2 -Members $username 
}

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
Introducing the "443 Security Simplified" Podcast

This new podcast puts you inside the minds of leading white-hat hackers and security researchers. Hosts Marc Laliberte and Corey Nachreiner turn complex security concepts into easily understood and actionable insights on the latest cyber security headlines and trends.

vpetersenAuthor Commented:
Thanks, I'm sure that's from me changing everything a dozen times and forgetting to update the first variable. Still doesn't work for me though.

$users = import-csv c:\temp\users.csv 
foreach ($user in $users) {
$email = $_.email
write-host $email }

Open in new window

0
RobSampsonCommented:
You can either use:
$users = import-csv c:\temp\users.csv 
foreach ($user in $users) {
$email = $user.email
write-host $email }

Open in new window


Or to use the $_ variable, you can use:
import-csv c:\temp\users.csv |
foreach {
$email = $_.email
write-host $email }

Open in new window


Rob.
0
RobSampsonCommented:
Technically, you can also use
$users = import-csv c:\temp\users.csv
$users |
foreach {
$email = $_.email
write-host $email }

Open in new window


but the second above is shorter.

Rob.
0
vpetersenAuthor Commented:
The $users was the problem and the fact that I had changed my CSV header and forgot. Thanks Rob!
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.

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.