remove Custom Attribute for users not in group

Jakob Digranes
Jakob Digranes used Ask the Experts™
on
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
    {

    }
}

?????
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014
Commented:
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

Author

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?

Author

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 !
Top Expert 2014

Commented:
Super!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial