Please help me out. What I'm doing is importing 16 AD groups in a PowerShell script and finding the list of users in those AD groups. I am doing this for audit purpose.
Since I'm exporting the user list for 16 AD groups, the -append parameter is making the list of users in the output file appear one next to it and I couldn't figure out which user belongs to which group, since there are 450 to 500 users in the output file. I need some changes in the below script so that each user appears with the group name for which it is extracted.
Example- script run for first AD group- lists 4 users in that AD group. The script runs again for another AD group, this should display with the users' list along with the Group name which is trying to export the list. Hope I am not complicating it. Please help me out.
$groups = Get-content -path "C:\Users\Santosh\OBIP.csv"
foreach ($GroupName in $groups)
{
Get-ADgroupmember -Id $GroupName -Recursive | Where objectclass -eq 'user' | Get-ADUser -Properties Displayname,GivenName,Surname,UserPrincipalName,Title,Department,Enabled,ObjectClass, memberof | Select DistinguishedName, samAccountName, Name, Displayname, GivenName, Surname, UserPrincipalName, Title, Department, Enabled, ObjectClass | export-csv -path "C:\Users\Santosh\OBIP\GN.csv" -Append
}