Ad group Member validation

Wanted to check if user is a part of an AD group
Script should help to get info for multiple users at once

Below script is not working as its shows do not exists even if the user is a part of the group:

$user = "xxxxxx"
$group = "xxxxxxxxxx"
$members = Get-ADGroupMember -Identity $group -Recursive | Select -ExpandProperty Name

If ($members -contains $user) {
      Write-Host "$user exists in the group"
 } Else {
        Write-Host "$user not exists in the group"
}
Pavan JoshiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Derek SouterITO Svc Delivery Cons IIICommented:
have you tried simply listing all the members of the group that are returned?    I would suggest that it may be showing username as domain\username - and you are not necessarily checking for that.
1
Pavan JoshiAuthor Commented:
For any ID it gives the same result... Even if the user is not a part of that group
0
Pavan JoshiAuthor Commented:
Any other script I can try
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

oBdACommented:
That probably happens because you're using the Name property, instead of SamAccountName, which must be used in this case.
Try it like that:
$verifyUsers = 'User1', User2', 'User3'
$verifyGroup = 'Group123'

$groupMembers = Get-ADGroupMember $verifyGroup -Recursive | Select-Object -ExpandProperty SamAccountName
Compare-Object -ReferenceObject $verifyUsers -DifferenceObject $groupMembers -IncludeEqual |
	Where-Object {$_.SideIndicator -ne '=>'} |
	Select-Object -Property @{n='GroupName'; e={$verifyGroup}}, @{n='UserName'; e={$_.InputObject}}, @{n='IsMember'; e={$_.SideIndicator -eq '=='}}

Open in new window

2

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
Pavan JoshiAuthor Commented:
Wow, @Obda
It worked but wanted to know if I can use a file as content where all IDs are present and just run the script
0
oBdACommented:
The script doesn't care where the users are coming from.
Assuming you have one SamAccountName per line in a text file, you can just use something like
$verifyUsers = Get-Content -Path 'C:\Temp\users.txt'
# ...

Open in new window

2
Pavan JoshiAuthor Commented:
Thanks oBdA
The script works now and I was able to get the output as required.
This is one forum where I get maximum help and I am always thankful to you all :)
0
Pavan JoshiAuthor Commented:
Thanks oBdA
The script works now and I was able to get the output as required.
This is one forum where I get maximum help and I am always thankful to you all :)
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.