Link to home
Start Free TrialLog in
Avatar of P S
P S

asked on

Need a script to see if requested samaccountnames are part of a particular group or not

I have a csv file with around 5000+ samaccountnames in it. I need to check which accounts are part of a security group named "TestGroup". The output CSV file should write the status as "TRUE" or "FALSE" if a certain account is not part of the group.

Please help.

Thanks
Avatar of Alex
Alex
Flag of United Kingdom of Great Britain and Northern Ireland image

I think this should work for you.

Import-CSV $File |
ForEach {
$users = $_.User;
$group = $_.Group;


$members = Get-ADGroupMember -Identity "$group" -Recursive | Select -ExpandProperty SAMAccountName 
IF($members -contains $users) {Write-Host "$users in $group"} 
Else { Write-Host "$users not in $group"}
}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mahesh
Mahesh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of P S
P S

ASKER

Thanks Mahesh