Created
October 1, 2018 22:35
-
-
Save dave-britten/8046ffe72a0c072b1220017172e6e516 to your computer and use it in GitHub Desktop.
Compares group membership of 2 or more users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Compare-ADGroupMembership { | |
[CmdletBinding()] | |
Param ( | |
[Parameter(Mandatory=$true,ValueFromPipeline=$true)] | |
[Alias('Name')] | |
[string[]]$Username | |
) | |
BEGIN { | |
$groups = @{} | |
} | |
PROCESS { | |
ForEach ($u in $Username) { | |
$membership = Get-ADPrincipalGroupMembership -Identity $u | |
foreach ($g in $membership) { | |
Write-Verbose "Group: $($g.name)" | |
if (!$groups.ContainsKey($g.name)) { | |
$groups.Add($g.name,[System.Collections.ArrayList]@()) | Out-Null | |
} | |
$groups[$g.name].Add($u) | Out-Null | |
} | |
} | |
} | |
END { | |
ForEach($g in $groups.GetEnumerator()) { | |
$o = New-Object -TypeName PSObject | |
$o | Add-Member NoteProperty GroupName $g.Name | |
ForEach($u in $Username) { | |
$o | Add-Member NoteProperty $u (@{$true=$u;$false=''}[$g.Value -contains $u]) | |
} | |
Write-Output $o | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment