Powershell Active Directory - array output formatting help

garryshape
garryshape used Ask the Expertsâ„¢
on
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... 😪
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Your described output format is only good for console output, not really for export and further processing.
This generates the export, then sends it to the console in groups, and to a csv.
$Distros = Get-ADGroup -Filter "Name -like 'Emergency - *'"
$report = $Distros | ForEach-Object {
	Write-Host "Processing $($_.Name)"
	$group = $_
	$group |
		Get-ADGroupMember -Recursive |
		Where-Object {$_.ObjectClass -eq 'user'} |
		Get-ADUser -Properties Title, Department |
		Select-Object -Property @{n='Group'; e={$group.Name}}, SamAccountName, Title, Department |
		Sort-Object -Property SamAccountName
}
$report | Format-Table -GroupBy Group -Property SamAccountName, Title, Department -AutoSize
$report | Export-Csv -NoTypeInformation -Path C:\Temp\Distros.csv

Open in new window

Sorry for delay thank you so much

Do more with

Expert Office
Submit tech questions to Ask the Expertsâ„¢ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial