remove Custom Attribute for users not in group

HI Powershell Guruz; I received some help for the script in the thread below.
http://www.experts-exchange.com/questions/28827119/need-to-set-CustomAttribute-based-on-mailboxGUID.html

No I need to somewhat reverse it, need to get all user who are not a member of the but, but have value in -CustomAttribute1 - and after that clear CustomAttribute for those users --- any ideas?


$users = Get-ADGroupMember Test_MBX_Permission | Select-Object -ExpandProperty name
foreach ($user in $users)
{
    $userinfo = Get-Mailbox -Identity $user -ErrorAction SilentlyContinue | select
    if NOT A MEMBER OF THE GROUP
    {
        #If customattribute1 have value - SET VALUE TO Null
    }
    else
    {

    }
}

?????
LVL 23
Jakob DigranesSenior advisorAsked:
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.

footechCommented:
Give this a shot.
# Members of the group
$ge = Get-ADGroupMember "Test_MBX_Permission" -Recursive | Select samaccountname

# The potential pool of all users
$ga = Get-ADUser -Filter * -properties CustomAttribute1 | Select samaccountname

# Compare to determine users which aren't members.
$users = Compare-Object $ga $ge -Property samaccountname | Where {$_.SideIndicator -eq "<="} | Select -ExpandProperty samaccountname
foreach ($user in $users) 
{
    $userinfo = Get-Mailbox -Identity $user -ErrorAction SilentlyContinue | select CustomAttribute1,GUID
    if ($userinfo.CustomAttribute1)
    {
        Set-Mailbox -Identity $user -CustomAttribute1 $null
    }
}

Open in new window

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
Jakob DigranesSenior advisorAuthor Commented:
That one seemed to the trick. However, it seems now that users can be member of 3 groups. Is that even possible? to get it to traverse through 3 groups and then check?
Jakob DigranesSenior advisorAuthor Commented:
got it ... All groups start with the same name; so changed like this:

$ge = Get-ADGroup -LDAPFilter "(name=G_Email_archive*)" | Get-ADGroupMember -Recursive | Select samaccountname

Thanks !
footechCommented:
Super!
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
Exchange

From novice to tech pro — start learning today.