troubleshooting Question

Powershell Active Directory - array output formatting help

Avatar of Garry Shape
Garry ShapeFlag for United States of America asked on
PowershellActive Directory
2 Comments1 Solution98 ViewsLast Modified:
I am working on a Powershell script that will go through the memberships of AD groups that begin with a certain name and then output the group name along with only unique Titles and Departments of members.

So for example if there are two people in a group with the same title and department, I'm aiming to get it to output like this, where it has group name, and then the Title column and Department column with unique ones.

Now I could do this to Excel if that works and just remove duplicates to make it easier.
But the issue I'm having is that the Title and Department values always output as an array.

Results style I'd like to yield:

Group name: "Emergency - All"

Title               |    Department
IT Admin       |   IT
Technician    | Facilities

Group name: "Emergency - Facilities"

Title               |    Department
Technician    | Facilities

And so on and so forth for each group....

How results are currently yielding:

Title                                                Department
-----                                                  ----------
{IT Admin , Technician...} {Facilities...

Obviously that creates a problem for both readability and exporting.
It's an array with commas and I don't know how to overcome it.

The script I currently have is:  
$report = @()
$Distros = Get-ADGroup -filter * | ? {$_.Name -like "Emergency - *"}
    foreach ($Distro in $Distros){
        $DistroMembers = Get-ADGroupMember -Identity $Distro.SID -Recursive | Where objectclass -eq 'user' | Get-ADUser -Properties Displayname,Title,Department | Select DistinguishedName,samAccountName,Name,Displayname,Title,Department 
            #add line to object

            $obj = New-Object -TypeName PSObject
            $obj | Add-Member -MemberType NoteProperty -Name Group -Value $Distro.Name
            $obj | Add-Member -MemberType NoteProperty -Name Title -Value ($DistroMembers | select Title -unique).title
            $obj | Add-Member -MemberType NoteProperty -Name Department -Value ($DistroMembers | select Department -unique).department

            $report += $obj


And then when i run $report, I get the array output problem... 😪
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros