use output of one command as input for another

The membership of (example) the domain admins group which I get by the following command

Get-ADGroupMember-Identity "Domain Admins" -Recursive|select-objectsamaccountname

Should be used to add these users to another group. Which command do I need to use in the script to add these users to another group ?

September 3rd, 2015 6:45am

It always depends on situation - in this case you case use

get-adgroupmember -identity "Domain Admins" -recursive | % { add-adgroupmember -identity NewGroup -members $_ }

on the other hand you could do get-aduser -filter * | set-aduser -manager Administrator -whatif

Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 6:59am

You can put the result of any executions also in variables if you are not sure when to use the piping.

For example:

$admember=Get-ADGroupMember-Identity "Domain Admins" -Recursive|select-objectsamaccountname

$group = Get-ADGroup -LDAPFilter "(name=*TestGruppe*)" -SearchBase "OU=Infra,DC=contoso,DC=de"
if ($group -ne $null )
{
   Add-ADGroupMember $group -Members $admember
}
The pro of this way is that you can easily debug the script by proving what it is in the variables, going through the code step by step by using the "F9"-key etc.
September 3rd, 2015 7:12am

The easiest way is to just use the -Members parameter of Add-ADGroupMember.


Add-ADGroupMember -Identity 'Other Group' -Members (Get-ADGroupMember -Identity 'First Group' -Recursive)

http://ss64.com/ps/add-adgroupmember.html

Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 7:29am

Hi Mike,

As I can see the -Member parameter of Add-ADGroupMember accept objects , but they should be identified

by (let's say) SAM account name. The part of your example "..... Get-ADGroupMember -Identity 'First Group' -Recursive)" dreturn the hole objects, not a property values (like SAMaccountName) separated by commas.

I just can't figure it out and asking for an advice. If your example is working on you so maybe there is something wrong on my site :) Sorry if that's the case!!!

I use this one to get a comma-separated list of group members:

(Get-ADGroupMember -Identity "GROUP1" -Recursive | select-object
samaccountname -ExpandProperty samaccountname) -join ","

But when I add this command in a double parenthesis after

Add-ADGroupMember -Identity 'Other Group' -Members
it still doesn't work. Returning an error that It cant find an objects with such identities. It would be great if you can give an advise. One more time if - if your command is working on your side , please excuse me and ignore my post.

September 3rd, 2015 8:38am

Hi,

What I've posted works just fine. I use it all the time.

Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 8:44am

You guys are brilliant.

Thanks so much.

September 3rd, 2015 8:45am

Cheers, you're very welcome. Glad it worked out.
Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 8:51am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics