Powershell Script to Pull all the AD Group in Domain

Team, Need help on getting Powershell Script to Pull all the AD Group in Domain. I need the below output in report.

- Groups name with users count in entire domain
- Last modified Date
LVL 26
Sekar ChinnakannuStaff EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

$GroupArray = Get-ADGroup -Properties * -Filter * -SearchBase "DC=corp,DC=yourcompany,DC=local"
Foreach($G In $GroupArray)
    Write-Host $G.Name
    Write-Host "-------------"

Pull all of these into a excel and do a pivot.
Sekar ChinnakannuStaff EngineerAuthor Commented:
I am getting all users, computers as output, I am looking for a script to generate all groups in a domain.

- AD Group name with users count in entire domain
- Last modified Date
Abdul Khadja AlaoudineTechnical ConsultantCommented:
Below script will give you list of AD groups with memeber count in each group and modifed date for the groups.

$GroupArray = Get-ADGroup -Properties Modified, Members -Filter * -SearchBase "DC=yourcompany,DC=local"
Foreach($Group In $GroupArray)
     Write-Host "Group Name:"$Group.Name
     Write-Host "Total members:"($Group.Members).count
     Write-Host "Modified date:"$Group.modified

To get total number of users in domain

$users = Get-ADUser -Filter * -SearchBase "DC=yourcompany,DC=local"

To get list of users and their modified date

$UserArray = Get-ADUser -Filter * -SearchBase "DC=yourcompany,DC=local" -Properties Modified
Foreach($User In $UserArray)
     Write-Host "User Name:"$User.Name
     Write-Host "Modified date:"$User.modified
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Sekar ChinnakannuStaff EngineerAuthor Commented:
Also can you please help me to get output in CSV file
Abdul Khadja AlaoudineTechnical ConsultantCommented:
Looks like you added 'To get total number of users in domain' line to the script. Remove it, save the ps1 file and try again.
Sekar ChinnakannuStaff EngineerAuthor Commented:
Its working fine, can you help me to add OU details for the group and same need to generate as csv file like below format.

Group Name, Users Count, Modified Date, OU
Abdul Khadja AlaoudineTechnical ConsultantCommented:
Here we go...

$GroupArray = Get-ADGroup -Properties Modified, Members -Filter * -SearchBase "DC=yourcompany,DC=local"
$GroupArray | Select Name,@{Name="Users Count";Expression={($_.members).count}},Modified,DistinguishedName | Export-CSV "Groups.csv"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.