Compare-SecurityGroups.psm1
function Compare-SecurityGroups { [cmdletbinding()] param( [Alias("Identity")] [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [string[]]$SamAccountName ) begin { $userAll = [System.Collections.Generic.List[object]]::new() } process { try { foreach ($sam in $SamAccountName) { $userObj = Get-ADUser -Identity $sam -Properties MemberOf -ErrorAction Stop $userAll.Add($userObj) } } catch { Write-Host $_.Exception.Message -ForegroundColor Red; return} } end { $secGroupAll = $userAll | ForEach-Object { $_ | Select-Object -ExpandProperty MemberOf } | Select-Object -Unique | Sort-Object foreach ($secGroup in $secGroupAll) { $outputObj = [PSCustomobject]@{ SecurityGroup = ($secGroup -split ',')[0] -replace 'cn=','' } foreach ($user in $userAll) { $outputObj | Add-Member -MemberType NoteProperty -Name $user.SamAccountName -Value ($user.MemberOf -contains $secGroup) } $outputObj } } } |