If you don't need the vendor's "description" in the final output, try this script. I'm still not sure what your CSV requirements are so you can fiddle around with how you deal with the data. Just keep in mind that a CSV must have the same number of columns in every row and that the column headers must be unique.
A disclaimer -- I don't have an AD to work with so while the "logic" may be correct there may be some adjustment needed to the code!
$ou = Get-ADOrganizationalUnit -Identity "OU=Groups,OU=Enterprise Support,DC=mydomain,DC=local"
$GroupDNs = @{}
Get-ADGroup -Filter "*" -SearchScope onelevel -SearchBase $ou.distinguishedname | # "Enterprise Support/Groups"
ForEach-Object{
if (-not $GroupDNs.ContainsKey($_.distinguishedname)){
$GroupDNs[$_.distinguishedname] = @()
}
}
Get-Content c:\BAS\grouplist2321.txt |
ForEach-Object {
Get-ADGroupMember $_ | # NOTE: This will return Users, Group, Computers, etc., so Get-ADUser may throw errors
ForEach-Object {
if ($u = Get-ADUser -properties Description -Filter {Description -like "Vendor"}){
$u.MemberOf |
ForEach-Object{
if ($GroupDNs.ContainsKey($_){
$GroupDNs[$_] += $u.name
}
}
}
}
}
# make a CSV with two columns: GroupName and Vendors
# the Vendors colum will be a semi-colon separated list
$GroupDNs.GetEnumerator()|
ForEach-Object{
[PSCustomObject]@{
GroupName = (Get-ADGroup -Identity $_.Key).Name
Vendors = $_.Value -join ";"
}
} | Export-Csv c:\junk\vendors.csv -NoTypeInformation