Function to display username once and devices of user from Device Afinity

I’m working on a powershell script that gets the device affinity of the user to the device. i could have, for example, one user attached to 4 devices. I want to create a small function to display the users and the device. But here’s thing, i want to display the username once and the devices associated with the user.
Currently all i get back is the same username, for example, 4 times for each device. This is what i have :

$devices = Get-CMDevice -CollectionName 'Visio 2010 Multiple Devices'
$Return = $devices | foreach { @( Get-CMUserDeviceAffinity -UserName $_.UserName) }
$Return | % { Write-Output "$($_.UniqueUserName | select -Unique ) : $($_.ResourceName)" }

How can i achieve this ?


So, not being familiar with the CM commands, I guess I’m not sure what output you ARE getting and what output you WANT to get. Can you provide examples?