Using multiple loops in AD powershell

Varughese Kochukalical Eappen 21 Reputation points
2021-07-18T11:16:43.57+00:00

I have to add users to some groups based on their departments. There may be more than 1 group per department.
So most groups which are specific to a department will have the department mentioned in the department attribute for others this field may be empty.

Similarly users in the department also have department mentioned in a custom attribute called departmentnumber. For a a group belonging to a department the department value will be same as departmentnumber attribute for the user.

I use the below command to get the list of groups which are specific to a single department
Get-ADGroup -Filter {department -like "*"} -Property department

Furthermore if I want the list of users who belong to these departmentsm, I can get using the below script
Get-ADGroup -Filter {department -like "*"} -Property department | ForEach {Get-ADUser -Filter {departmentnumber -like $_.department}}

I run into an error in the below script
(Save array of Department groups in a variable and Counter through groups and users who have same department attribute)
$GP=Get-ADGroup -Filter {department -like "*"} -Property department
ForEach {$GP+
Get-ADUser -Filter {departmentnumber -like $GP.department} | ForEach{Add-ADGroupMember -Identity $GP.SamAccountName -Members $_.SamAccountName}
}

Can someone poibt out what is wrong. Is Gp+ the incorrect way to increment the array counter?

Error is:
At line:1 char:8

  • ForEach {$GP+
  • ~
    Missing opening '(' after keyword 'foreach'.
    At line:1 char:14
  • ForEach {$GP+
  • ~
    You must provide a value expression following the '+' operator.
    At line:2 char:1
  • Get-ADUser -Filter {departmentnumber -like $GP.department} | ForEach ...
  • ~~~~~~~~~~
    Unexpected token 'Get-ADUser' in expression or statement.
  • CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
  • FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword
Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2021-07-18T14:37:16.403+00:00

    "ForEach {$GP+" is just wrong!

    What I think you meant to do is this:

    Get-ADGroup -Filter {department -like "*"} -Property department |
        ForEach-Object {
            $GPSamAccountName = $_.SamAccountName
            Get-ADUser -Filter {departmentnumber -like $_.department} | 
                ForEach-Object {
                    Add-ADGroupMember -Identity $GPSamAccountName -Members $_.SamAccountName
                }
        }
    

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2021-07-20T02:53:41.88+00:00

    Hi,

    The ForEach loop can be like below.

    $GPs=Get-ADGroup -Filter {department -like "*"} -Property department  
    ForEach ($GP in $GPs){  
        Get-ADUser -Filter {departmentnumber -like $GP.department} |   
            ForEach-Object{Add-ADGroupMember -Identity $GP.SamAccountName -Members $_.SamAccountName}  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.