Need a script to read group names in a list. Find users in each group with "vendor" as Description output to a CSV thei group name and it members.

MMitschke 1 Reputation point
2021-02-05T21:48:02.23+00:00

Running this:
$grouplist = get-content c:\BAS\grouplist2321.txt
foreach ($group in $grouplist)
{
get-adgroupmember $group
get-aduser -properties Description -Filter "(Description -like "Vendor") -and (MemberOf -like "Enterprise Support/Groups")" | Select-Object name,description,memberof | Sort memberof | Export-Csv c:\BAS\Group.csv -notypeInformation}

The output comes back with the column headers Name ... Description. However I get all the names in the group. It's filtering just those with Vendor in their Description field.

Windows for business Windows Server User experience PowerShell
{count} votes

6 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-02-09T16:09:13.363+00:00

    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
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.