Powershell – Power shell script Get-ADGroup member and owner

active-directorypowershellshell-scripting

I'm having some problems with my powershell script to get ADGroups from our AD. I want the information about what the group name, description of the group, members in the groups and the owner of the group.

I have this script written allready

$GruppeMedlemmer = @()
$Groups = Get-ADGroup -Filter * -properties * -SearchBase "OU=butikk,OU=Grupper,OU=Costumer,DC=OO,DC=NN"
foreach ($g in $Groups) {
$members = $g | Get-ADGroupMember 
     foreach ($m in $members) {
       $Info = New-Object psObject 
       $Info | add-member -MemberType NoteProperty -Name "GroupName" -Value $g.Name
       $Info | add-member -MemberType NoteProperty -Name "Description" -Value $g.description
  $Info | add-member -MemberType NoteProperty -Name "Member of" -Value $g.MemberOf
       $Info | Add-Member -MemberType NoteProperty -Name "Member" -Value $m.name    
       $GruppeMedlemmer+= $Info
     }
}
$GruppeMedlemmer | Sort-Object GroupName | Export-CSV C:\data\groupdist.csv -notype -Encoding unicode 

This script gives me the group names, description and members of the group, but I'm not sure what to add to get the group owner.

Can I add a new line "managedby" -Value $m.name ? I must be writing something wrong, because when I try to add new line to get managedby it only fails.

Can somebody help me with the right way to implement this in my script?

Thank you!

Best Answer

Can I add a new line "managedby" -Value $m.name ?

ManagedBy is a property of Get-ADGroup so in your script you have to use $g.ManagedBy

$Info | Add-Member -MemberType NoteProperty -Name "ManagedBy" -Value $g.ManagedBy

BTW It would help to know which PowerShell version you use.