We help IT Professionals succeed at work.
Get Started

Powershell Active Directory - array output formatting help

garryshape asked
1 Endorsement
Last Modified: 2020-01-13
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... 😪
Watch Question
Most Valuable Expert 2019
Most Valuable Expert 2018
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE