Avatar of Garry Shape
Garry Shape
Flag for United States of America asked on

Powershell Active Directory - array output formatting help

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

    }

Open in new window


And then when i run $report, I get the array output problem... ūüė™
PowershellActive Directory

Avatar of undefined
Last Comment
Garry Shape

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Garry Shape

ASKER
Sorry for delay thank you so much
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes