Get users Group and members of the groups

$AllUSers = get-aduser -filter {SamAccountName -like “z*” -and Enabled -eq $true } -Properties Memberof,EmployeeNumber -Server <Domain name> | ?{$PSItem.DistinguishedName -notmatch “OU=Service Accounts”}

$results = new-object System.Collections.ArrayList

foreach($user in $AllSers){

$grps = $user.MemberOf

$v = New-Object System.Collections.ArrayList

foreach($grp in $grps){

$Gobj = Get-ADGroup $grp -Server <Servername:3268> -Properties GroupCategory,GroupScope,Name | select @{l=”GroupName”;e={(($PSItem.DistinguishedName -split “,DC=”)[1])+”\”+$PSItem.SamAccountName}},GroupCategory,GroupScope

$l = $Gobj.GroupName+”,”+ $Gobj.GroupScope +”,”+ $Gobj.GroupCategory

$v.Add($l)

}

$o = [pscustomobject]@{

SamAccountName = $user.SamAccountName

#ProdEmail = $user.EmployeeNumber

Name = $user.GivenName + ” ”  +$user.Surname

DevUPN = $user.UserPrincipalName

‘Groups(Name,Scope,Category)’ = $v -join “;”

“Memberof(RawData)” = $user.MemberOf -join “;”

}

$results.Add($o)

}

$results | export-csv C:\Temp\User_Groups.csv -NoTypeInformation

4 Comments

  1. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You obviously know what youre talking about, why waste your intelligence on just posting videos to your blog when you could be giving us something enlightening to read?

Leave a Reply

Your email address will not be published.