Verify if Account is in a certain AD group


I use the search below to get me certain accounts within my environment , can someone help me modify it so it can also verify
if these "x" accounts are also members of the  " X Users Group"  ? so the xData file can also show if each of these accounts are members
of the X Users Group ?

Get-ADUser -Filter {SamAccountName -like "*X"} -properties * | ? { $_.whenCreated -ge (get-date "December 1, 2014") -and ($_.samaccountname.length -eq 10) } | Select SamAccountName,description,Title | Export-csv "xData_$(Get-date -f dd-MM-yyy-hhmmss).csv" -NTI
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.

Phil BossmanSenior Client Systems AdminstratorCommented:
I've reformatted your code slightly so I can better understand it and explain what I've edited

I start with getting the group you want to query. Then your all of your code.   I've added to the Select a dynamic field "XGroup" which will contain the "X GroupName" if the user is a member of the group
Get-ADGroup -Filter { member -RecursiveMatch $_.DistinguishedName } -SearchBase $ADGroup.DistinguishedName | Select -ExpandProperty Name

This code will get an AD Group by searching for a group where "X user" is a member recursively, (meaning the user can also be a member of any subgroup).  The search will start at the original AD Group, so we're not returning all groups that "x users" is a member.  If the user is found, then the group name is returned to the "XGroup" field by the select statement.  When the user is not found, the "XGroup" field is left blank.

$ADGroup= Get-ADGroup -Identity "X Users Group"

Get-ADUser -Filter {SamAccountName -like "*X"} -properties * |
    Where-object { $_.whenCreated -ge (get-date "December 1, 2014") -and ($_.samaccountname.length -eq 10) } |
        Select SamAccountName,Description,Title,@{Name='XGroup';Expression={ Get-ADGroup -Filter { member -RecursiveMatch $_.DistinguishedName } -SearchBase $ADGroup.DistinguishedName | Select -ExpandProperty Name } } |
            Export-csv "xData_$(Get-date -f dd-MM-yyy-hhmmss).csv" -NoTypeInformation

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
MilesLoganAuthor Commented:
Hi Phil , this worked perfect .. thank you so much !
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

From novice to tech pro — start learning today.