using a pipeline to pass a member into Add-ADGroupMember

Command Example

Get-ADGroup “GROUP1” | Add-ADGroupMember -Identity “GROUP2” -Members $_.ObjectGUID


Add-ADGroupMember : Cannot validate argument on parameter ‘Members’. The argument is null or empty. Provide an argument that is not null
or empty, and then try the command again.


So unfortunately I have written like 400 lines of these commands and I know I can change the command to this but that means modifying roughly 400 lines of code. Any idea what I’m doing wrong? I have tried multiple ways of entering the pipeline data without success.

I could probably modify the code to do a foreach down to about 100 lines of code but still modifying a ton of code. For me easier to just find a way to do a find and replace if it is possible.


Any reason you can’t do this instead?

Add-ADGroupMember -Identity "GROUP2" -Members (Get-ADGroup "Group1").DistinguishedName

I would also look at adding error handling to your script i.e.

if ($null -ne $Variable){
    Complete the action

Also try this:


[pre]Get-ADGroupMember -Identity GROUP-A | Add-ADPrincipalGroupMembership -MemberOf GROUP-B[/pre]

Jason, yes I can do that there are a few different ways to skin the cat I just need the easiest for a modification of a ton of code. I read somewhere about doing the pipeline on MS and I think it was in relation to using a user account or something and thought the group would work just fine. So wrote all this code then to see it is not working :(. Lesson learned I guess.

I rebuilt the code by doing the code change to use the foreach to cut the amount of work needed in the long run and used some excel tricks with concatenation to build what I needed after doing some splits. Thanks everyone for the help.

I would still be interested in knowing why I can’t pass $_ to the -Members as a value if someone knows the answer.

Best explanations I’ve found are here and here.

“Same as $PSItem. Contains the current object in the pipeline object. You can use this variable in commands that perform an action on every object or on selected objects in a pipeline.”

“The current pipeline object; used in script blocks, filters, the process clause of functions, where-object, ForEach-object and switch”

I’ve only ever used it in a ForEach-object or a Where-Object. I don’t think it’s available outside of those constructs as it enumerates whatever is on the pipeline.


The $_ is used for the current pipeline object; used in script blocks, filters, the process clause of functions, where-object, ForEach-object and switch.

The correct method to use is one of the the following methods. The third method, my preferred method, is above in my original post.

$Member = Get-ADGroup "Group1"
Add-ADGroupMember -Identity "GROUP2" -Members $Member
Get-ADGroup "GROUP1" | Foeach-Object {Add-ADGroupMember -Identity "GROUP2" -Members $_.ObjectGUID}

I recommend reading the help file for the parameters you want to use in a cmdlet. Take for instance the Members parameter for Add-ADGroupMember notice the Accept Pipeline input is set to false. What this means is the parameter won’t accept the value for members from another object. Even if you didn’t need to use the $_ place holder.

help Add-ADGroupMember -Parameter Members | Select-String Accept

Accept pipeline input? false
Accept wildcard characters? false