Q: Loop issue, script not adding the correct members to groups Ok so code first:
 import-module activedirectory

. "C:\Users\ben\Documents\Get-Directreport.ps1"

$ou = "ou=test, ou=Litmos, ou=Resources, ou=Groups, ou=company, dc=domain, dc=net"

$creds = "domain.net\ben"
$server = "dc01.domain.net"

$managers = get-adGroupMember -identity "CN=All Managers,OU=Organizational,OU=Groups,OU=company,DC=domain,DC=net" | select name, samaccountname
$name = $managers.name

$ReportsTo = Get-adgroup -searchbase $ou -filter "Name -like 'Report to *'" | where {$_.name -replace 'Report to ' -in $name} | select name, samaccountname
$Reports = $reportsto.name

$underlings = Get-Directreport $name | select samaccountname

Foreach ($manager in $name) { 
    if ($manager -notin ($reports -replace 'Report to ')) 
    { new-adgroup -name "Report to $manager" -groupscope global -path $ou }
 }   
 

ForEach ($report in $reports) {
    #Get-Directreport $name | select samaccountname
    {add-adgroupmember -identity $report -members $underlings.samaccountname}
} 

So this script is creating the 'Report to $name' groups correctly, and it's adding members however all 300 groups that this creates has the same set of members. I've tried putting the $underlings line inside the last loop but that resulted in no members at all.  I'm pretty sure that line is the culprit but I can't figure it out.
Thanks!
EDITS
First off apologies for the stuff I left out originally. I guess that's what I get for being in a hurry at the end of the day.  In any event, so this code is trying to create a series of new AD security groups, namely the "Report to x" mentions. It creates those folders by iterating over the members of teh "All Managers" group and the X variable will be each ALL Manager members name values.
Then I'm trying to populate those groups with the corresponding users. Which is where it's failing.
The line for Get-Directreport $name | select samaccountname Does in fact return what it should. I can run that entire line include $underlings in the terminal and it returns correctly.
I agree with Colyn1337 in that it's that line.. I'm unsure how to get it to iterate.

A: So after some time working through the script I've offered some changes and and inline comments.  Please have a look
import-module activedirectory

. "C:\Users\ben\Documents\Get-Directreport.ps1"

$ou = "ou=test, ou=Litmos, ou=Resources, ou=Groups, ou=company, dc=domain, dc=net"

$creds = "domain.net\ben"
$server = "dc01.domain.net"

$managers = get-adGroupMember -identity "CN=All Managers,OU=Organizational,OU=Groups,OU=company,DC=domain,DC=net" | 
    Select-Object name, samaccountname

# remove this and just use $managers.name.  No need to create this.  
## $name = $managers.name 

# Groups where name like "reports to $managersName"
$ReportsTo = Get-adgroup -searchbase $ou -filter "Name -like 'Report to *'" | 
    Where-Object { $_.name -replace 'Report to ' -in $name } | 
    Select-Object name, samaccountname

# Same as before.  No need for $Reports array.  Just use $ReportsTo.Name were needed
## $Reports = $reportsto.name  # AD Group names like "reports to <manager>'   


# list of  users for all managers?  Does Get-Directreport take an array of managers or does it expect only 1 manager?
# If sending multiple managers does work, which it sounds like it doesn't, but if it did you would have an array of all direct reports 
# for all managers in $name.  This should probably be moved into the foreach ($manager in $name) loop
## $underlings = Get-Directreport $name | Select-Object samaccountname  

Foreach ($manager in ($managers.Name))) { 
    # Creates  missing "Report to <manager>" groups
    if ($manager -notin (($ReportsTo.Name) -replace 'Report to ')) { 
        new-adgroup -name "Report to $manager" -groupscope global -path $ou 
    }

    # Get managers direct reports
    $underlings = Get-Directreports $manager | Select-Object samaccountname

    # Get manager's "report to <manager> group again to update memebers"
    $managerReportToGroup = Get-ADGroup -SearchBase $ou -Filter "Name -like 'Report to $manager'"
    if ($managerReportToGroup) {
        Add-ADGroupMember -identity $managerReportToGroup.Name -members $underlings.samaccountname
    } else {
        Write-Warning "Could not locate group for $manager"
    }

}   

# For each "reports to $managerName" group, adding direct reports found from Get-Directreports function/script
# $reports will not have any newly added groups from previous loop.  Would need to call Get-AdGroup again
# Recommend moving loop up into $manager loop
## ForEach ($report in $reports) {
##     #Get-Directreport $name | select samaccountname
##     { add-adgroupmember -identity $report -members $underlings.samaccountname }
## } 

