Help with Group-Object to find duplicates

Hello,

I am trying to use Powershell and Group-Object to identify users who have received two license packages. Unfortunately I can’t do this no matter what I change. I think the error is really stupid but I can’t find it. Could someone tell me what the error is and the solution?

# Einzelne Lizenzen für Microsoft 365 Lizenzen
$grp_u_Microsoft365_Lic_E1_Audio_Conferencing   = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E1_Audio_Conferencing"  -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E1 Audio Conferencing Lizenz
$grp_u_Microsoft365_Lic_E1_Exchange             = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E1_Exchange"            -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E1 Exchange Lizenz
$grp_u_Microsoft365_Lic_E1_Forms                = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E1_Forms"               -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E1 Forms Lizenz
$grp_u_Microsoft365_Lic_E1_OfficeMobileApps     = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E1_OfficeMobileApps"    -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E1 Office Mobile Apps Lizenz
$grp_u_Microsoft365_Lic_E1_SharePoint           = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E1_SharePoint"          -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E1 SharePoint Lizenz
$grp_u_Microsoft365_Lic_E1_Teams                = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E1_Teams"               -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E1 Teams Lizenz
$grp_u_Microsoft365_Lic_E1_TK_Full              = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E1_TK_Full"             -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E1 TK Full Lizenz
$grp_u_Microsoft365_Lic_E1_TK_Light             = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E1_TK_Light"            -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E1 TK Light Lizenz
$grp_u_Microsoft365_Lic_E3_Audio_Conferencing   = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E3_Audio_Conferencing"  -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E3 Audio Conferencing Lizenz
$grp_u_Microsoft365_Lic_E3_Exchange             = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E3_Exchange"            -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E3 Exchange Lizenz
$grp_u_Microsoft365_Lic_E3_Forms                = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E3_Forms"               -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E3 Forms Lizenz
$grp_u_Microsoft365_Lic_E3_OfficeProPlus        = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E3_OfficeProPlus"       -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E3 Office Pro Plus Lizenz
$grp_u_Microsoft365_Lic_E3_SharePoint           = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E3_SharePoint"          -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E3 SharePoint Lizenz
$grp_u_Microsoft365_Lic_E3_Teams                = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E3_Teams"               -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E3 Teams Lizenz
$grp_u_Microsoft365_Lic_E3_Test                 = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E3_Test"                -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E3 Test Lizenz
$grp_u_Microsoft365_Lic_E3_TK_Full              = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E3_TK_Full"             -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E3 TK Full Lizenz
$grp_u_Microsoft365_Lic_E3_TK_Light             = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_E3_TK_Light"            -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für E3 TK Light Lizenz
$grp_u_Microsoft365_Lic_P1_Exchange             = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_P1_Exchange"            -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für P1 Exchange Lizenz
$grp_u_Microsoft365_Lic_P2_Exchange             = Get-ADGroupMember -Identity "grp_u_Microsoft365_Lic_P2_Exchange"            -Recursive   | Get-ADUser -Properties UserPrincipalName # Gruppe für P2 Exchange Lizenz

# Lizenzpakete Exchange P1
$All_ExchangeP1_Users                           = @($grp_u_Microsoft365_Lic_P1_Exchange)

# Lizenzpakete Exchange P2
$All_ExchangeP2_Users                           = @($grp_u_Microsoft365_Lic_P2_Exchange)

# Lizenzpakete Office 365 E1
$All_Office365E1_Users                          = @($grp_u_Microsoft365_Lic_E1_Audio_Conferencing, $grp_u_Microsoft365_Lic_E1_Exchange, $grp_u_Microsoft365_Lic_E1_Forms, $grp_u_Microsoft365_Lic_E1_OfficeMobileApps, $grp_u_Microsoft365_Lic_E1_SharePoint, $grp_u_Microsoft365_Lic_E1_Teams, $grp_u_Microsoft365_Lic_E1_TK_Full, $grp_u_Microsoft365_Lic_E1_TK_Light)
$All_Office365E1_Users                          = $All_Office365E1_Users | Sort-Object UserPrincipalName -Unique

# Lizenzpakete Office 365 E3
$All_Office365E3_Users                          = @($grp_u_Microsoft365_Lic_E3_Audio_Conferencing, $grp_u_Microsoft365_Lic_E3_Exchange, $grp_u_Microsoft365_Lic_E3_Forms, $grp_u_Microsoft365_Lic_E3_OfficeProPlus, $grp_u_Microsoft365_Lic_E3_SharePoint, $grp_u_Microsoft365_Lic_E3_Teams, $grp_u_Microsoft365_Lic_E3_Test, $grp_u_Microsoft365_Lic_E3_TK_Full, $grp_u_Microsoft365_Lic_E3_TK_Light)
$All_Office365E3_Users                          = $All_Office365E3_Users | Sort-Object UserPrincipalName -Unique

# Alle Lizensierte Benutzer
$All_Licensed_Users                             = @($All_Office365E1_Users, $All_Office365E3_Users, $All_ExchangeP1_Users, $All_ExchangeP2_Users)


# Überprüfen Sie auf Duplikate
$duplikate = $All_Licensed_Users.UserPrincipalName | Group-Object | Where-Object { $_.Count -gt 1 } | Select-Object -ExpandProperty Name

if ($duplikate) {
    Write-Output "Folgende Benutzerobjekte sind in mehreren Lizenzpaketen enthalten: $duplikate"
} else {
    Write-Output "Alle Benutzerobjekte sind eindeutig."
}

Hi there,

Welcome and thank you so much for formatting your code, it’s greatly appreciated! One really helpful tip I can provide as a general troubleshooting thing to do is step through your code a line at a time and do checks (for example, checking the count of your vars) to make sure its outputting how you expect. I think your issue is how you are combining your groups. For example:

$All_Licensed_Users= @($All_Office365E1_Users, $All_Office365E3_Users, $All_ExchangeP1_Users, $All_ExchangeP2_Users)

I don’t think that’s doing what you expect. You are creating an array of 4 objects there. and each of those objects have child objects, which are the objects you care about. From the code, I think you’re expecting that to output a single collection of users, so it has X amount of users. If you check the count of that it’ll say 4, instad of your expected count of users. If you were to take this approach you probably need to adjust your logic of how you combine those groups I think you could be able to simply add the objects together instead everywhere you are trying to generate a list of users in a category. For example:

$All_Office365E1_Users = @($grp_u_Microsoft365_Lic_E1_Audio_Conferencing, $grp_u_Microsoft365_Lic_E1_Exchange, $grp_u_Microsoft365_Lic_E1_Forms, $grp_u_Microsoft365_Lic_E1_OfficeMobileApps, $grp_u_Microsoft365_Lic_E1_SharePoint, $grp_u_Microsoft365_Lic_E1_Teams, $grp_u_Microsoft365_Lic_E1_TK_Full, $grp_u_Microsoft365_Lic_E1_TK_Light)

becomes

$All_Office365E1_Users = $grp_u_Microsoft365_Lic_E1_Audio_Conferencing + $grp_u_Microsoft365_Lic_E1_Exchange + $grp_u_Microsoft365_Lic_E1_Forms + $grp_u_Microsoft365_Lic_E1_OfficeMobileApps + $grp_u_Microsoft365_Lic_E1_SharePoint + $grp_u_Microsoft365_Lic_E1_Teams + $grp_u_Microsoft365_Lic_E1_TK_Full + $grp_u_Microsoft365_Lic_E1_TK_Light

Definitely check the counts to ensure they align with what your expecting. Also apologies the group names are blending together in my head so if I missed something I apologize. I think you’ll probably understand what I’m getting at though. That way when you do your Group-Object on the UPN later, it properly groups on the UPN and you should get proper counts.

You are absolutely right. That was my mistake.
Thank you very much.

I am a newbie and am trying to find my way. Sorry if the question was stupid, but I just spent so many hours looking for the error and just didn’t know what to do.

No mistakes here, just happy accidents! In all seriousness, this is the exact kind of question this forum is for and it is not stupid at all! It was a great question to ask and others may come across this now and have a similar issue and see the solution and learn from it as well. The tip of stepping through your code I shared helps me a lot in troubleshooting so just wanted to share that with you, as I hope it’ll help you debug your future coding endeavors!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.