Add multiple security groups to computers using PowerShell

Posted on 2014-08-13
Last Modified: 2014-08-13
Help with script that can add multiple member groups to computers.

My script works if I only get one member group and assign it to a variable but when I assign multiple groups to a variable it doesn't like it. Someone please feel free to school me here.

# Computer Path to my computers in ADUC
$CP = "OU=folder,DC=domain,DC=com"

# Group Path to my member groups
$GP = "OU=Groups,DC=domain,DC=com"

# Groups that I want to add to computers
$SecGroup = Get-ADGroup -Filter {Name -like 'MemGroup1' -or Name -like 'MemGroup2' -or Name -like 'MemGroup3'} -SearchBase $GP

$C = Get-ADComputer -Filter 'Name -like "ComputerName"' -Properties * -SearchBase $CP

PS > Add-ADGroupMember $SecGroup -Members $C

Add-ADGroupMember : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADGroup' required by parameter 'Identity'. Specified method is not supported.

Open in new window

This works however if I change my $SecGroup variable to only grab one member group. But if I add more than one group then it breaks?

Question by:ryanmaves
    LVL 40

    Accepted Solution

    In the following Syntax.. You can see the the Identity parameter doesn't accept an array of values like Members parameter..
    Add-ADGroupMember -Identity <ADGroup> -Members <ADPrincipal[]>

    Open in new window

    You can use Foreach to loop the groups to assign membership..
    $SecGroup | %{Add-ADGroupMember $_.Name -Members $C}

    Open in new window


    Author Closing Comment


    I was trying to use the ForEach-Object cmdlet but looks like I was using it incorrectly. Thanks for the tip it even works if I assign multiple computers to $C, excellent!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    This article will show, step by step, how to integrate R code into a R Sweave document
    I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
    This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now