Skip to content

Instantly share code, notes, and snippets.

@dave-britten
Created October 1, 2018 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dave-britten/8046ffe72a0c072b1220017172e6e516 to your computer and use it in GitHub Desktop.
Save dave-britten/8046ffe72a0c072b1220017172e6e516 to your computer and use it in GitHub Desktop.
Compares group membership of 2 or more users
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