troubleshooting Question

Group Members to a csv file using PowerShell.

Avatar of Katia Nunez
Katia Nunez asked on
Active DirectoryPowershell
7 Comments1 Solution224 ViewsLast Modified:
Hello,

I have a script in PS that allows me to get a list of user in active directory and the groups those users are a member of.
What I have is this:

$OU= "OU=CIS Groups,OU=AllGroups,DC=xyz,DC=edu"
$groups = Get-ADGroup -Filter * -SearchBase $OU # Get list of Groups on CIS Groups
$output = ForEach ($g in $groups)
 {
 $results = Get-ADGroupMember -Identity $g.SamAccountname -Recursive | Get-ADUser -Properties displayname, SamAccountName, EmployeeNumber, Memberof
 
 ForEach ($r in $results){
 New-Object PSObject -Property @{

        GroupName = $g.Name
            GroupMember = $r.Memberof
        UserName = $r.SamAccountName
        ColleagueID = $r.EmployeeNumber
            FirstName = $r.GivenName
        LastName = $r.Surname
     }
    }
 }

$output | Export-Csv C:\GM.csv -NoTypeInformation

What I also need is to get the OU in which every AD user belongs. The partial result of the file is:
UserName      ColleagueID      LastName      GroupName              FirstName      GroupMember
MMX1111E      818523              Matlock            Astra Off-Campus      Michael       Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
JEM2222E      3426815        Jensen            Astra Off-Campus      Eric               Microsoft.ActiveDirectory.Management.ADPropertyValueCollection

In GroupMember, the information I want to see is the OU in which the groupName Astra Off-Campus is in.
In this case, an OU called Astra Access Control has the GroupName Astra Off-Campus and others in it.

Please, refer to attached file to see results.

Can someone tell me what can I do to get those results?
Example.csv
ASKER CERTIFIED SOLUTION
Ben Personick (Previously QCubed)
Lead SaaS Infrastructure Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros