Read Time:1 Minute, 16 Second
As an Active Directory Admin, we usually come to a situation where we have to find all the groups in an Organization Unit and the members of each group. I tried to accomplish this with a PowerShell Script and a cool html Output.
Script:
<# .Synopsis Find the Members of All the Group in the Defined OU .DESCRIPTION Find the Members of All the Group in the Defined OU. Dishtinguished Name is a Mandatory Parameter .EXAMPLE .\Group-MembersOU -DistinguishedName OU=Admin,DC=Domain,DC=local #> function Get-GroupMembersFromOU { [CmdletBinding()] Param ( #Please Provide the Distinguished Name for the OU [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] $DistinguishedName ) #Define CSS Style $head = @" <style> body { background-color:#FFFFFF; font-family:Arial; font-size:10pt; } td, th { border:1px solid DimGray ; border-collapse:collapse; } th { color:white; background-color:#0058a2; } table, tr, td, th { padding: 2px; margin: 0px } table { width:90%;margin-left:3px; margin-bottom:10px;} </style> <br> "@ # Define Report Location $VarLocation= '.\GroupMemberRep.html' # Delete Old files Remove-Item $VarLocation -ea SilentlyContinue # Define Organisation Unit $VarGrps = Get-ADGroup -Filter {ObjectClass -eq 'Group'} -SearchBase $DistinguishedName # Start Loop ForEach ($VarGrp in $VarGrps) { $VarMem = Get-ADGroupMember $VarGrp | Select Name, SamAccountName, DistinguishedName $obj= @{ 'GroupName' =$VarGrp 'GrpMembers'= $VarMem } $obj.GrpMembers | ConvertTo-Html -Title "Group Membership Report" -Body $obj.GroupName.Name -Head $head -As Table | Out-File $VarLocation -Append } #Show Output in HTML file Invoke-Expression $VarLocation } Get-GroupMembersFromOU
Output:
Dear Team,
Thanks for sharing valuable scripts.
Kindly advise which location out file will be generated?
I’ve executed this script on windows server 2008R2 standard.
Thanks for your valuable feedback. Currently, the report will be saved in the same location from where you are running the script. Path is defined in the variable.
$VarLocation= ‘.\GroupMemberRep.html’
You can update the path in variable $VarLocation
$VarLocation= ‘c:\temp\GroupMemberRep.html’
Mr.Shinish.
I have chagned below changes but while executeing I’m getting error.
#Please Provide the Distinguished Name for the OU
to
.\Group-MembersOU -DistinguishedName OU=virtual,DC=scada,DC=local
My Domain Name : virtual.scada.local
Please help
Here,I am using distinguished name “OU=Admin,DC=Domain,DC=local”, Where my domain name is “Domain.local” and the required OU is Admin. To find the Distinguished Name you can run the below cmdlet, where ‘admin’ is the required OU.
As per your domain “virtual.scada.local”, it should be OU=Sample,DC=virtual,DC=scada,DC=local
Example 1:
Get-ADObject -Filter {name -eq ‘ admin’} | Select Distinguishedname
Output:
Distinguished name
—————–
OU=Admin,DC=Domain,DC=local
Example 2:
Get-ADObject -Filter {name -eq ‘biz’} | Select Distinguishedname
Output:
Distinguishedname
—————–
OU=Biz,OU=Admin,DC=Domain,DC=local
OU=Biz,OU=Others,DC=Domain,DC=local