Need assistance to generate Groups report

I have 1400 groups in txt format. I am trying to generate the report(Groupname, GroupOwner’s Samaccoutname, Member’s Samaccountname,Member’s Mail and Member’s Displayname ).

When i put this line into variable it generates @{GroupOwner=Samaccountname}.

$ManagedbySamaccountname= $managedby.ManagedBy |Get-ADUser -Properties Samaccountname|select @{e={$_.Samaccountname};l='GroupOwner'}

when i run the same line without variable it works fine


GroupOwner

Samaccountname

Original Script

cls
$groups = Get-Content "C:\temp\Groups.txt"

foreach ($group in $groups)
{
$managedby =Get-ADGroup $group -Properties *

$ManagedbySamaccountname= $managedby.ManagedBy |Get-ADUser |select Samaccountname|select @{e={$_.Samaccountname};l='GroupOwner'}

$MembersDetails = $managedby.member |Get-ADUser -Properties Samaccountname,Mail,Displayname |select @{e={$group};l="GroupName"},Samaccountname,Mail,Displayname,($managedby.ManagedBy |Get-ADUser -Properties Samaccountname | Select @{e={$_.Samaccountname};l='GroupOwner'})

$MembersDetails

Break

}

Calculate the last property as well.

cls
$groups = Get-Content "C:\temp\Groups.txt"

foreach ($group in $groups) {
    $managedby = Get-ADGroup $group -Properties *
    $GroupName = @{e = { $group }; l = "GroupName" }
    $GroupOwner = @{E = { $managedby.ManagedBy | Get-ADUser -Properties Samaccountname | Select-ExpandProperty Samaccountname }; l = 'GroupOwner' }
    $managedby.member | Get-ADUser -Properties Samaccountname, Mail, Displayname | Select-Object -Property $GroupName, Samaccountname, Mail, Displayname, $GroupOwner
    Break
}

Thanks Kvprasoon. I tried your script but it is not generating groupowner value from AD. It is showing blank but owner is there in AD.

I didn’t test them, Try this.

cls
$groups = Get-Content "C:\temp\Groups.txt"

foreach ($group in $groups) {
    $managedby = Get-ADGroup $group -Properties *
    $GroupName = @{e = { $group }; l = "GroupName" }   
    $Detail = $managedby.member | Get-ADUser -Properties Samaccountname, Mail, Displayname

    $GroupOwner = @{E = { $managedby.ManagedBy | Get-ADUser -Properties Samaccountname | Select-Object -ExpandProperty Samaccountname }; l = 'GroupOwner' }
    $Detail | Select-Object -Property $GroupName, Samaccountname, Mail, Displayname, $GroupOwner
    Break
}

Did not help. We are getting this output from $groupowner variable.

Name Value


l GroupOwner
E $managedby.ManagedBy | Get-ADUser -Properties Samaccountname | Select-ExpandProperty Samaccountname

$GroupOwner is a hashtable and is used to calculate properties using Select-Object cmdlet. The calculated value will come only when used with Select-object.

same is happening for $GroupName,

$Detail | Select-Object -Property $GroupName/pre>

Make sure $managedby.ManagedBy has some value, else GroupOwner will be null.