title: Add OU of users / computers to a security group
body: <p>I am trying to do 2 things. </p>
  
  <ol>
  <li>Add all users in an OU to a security group </li>
  <li>Add all computers in an OU to a security group </li>
  </ol>
  
  <p>I can successfully get a list of the users or computers I need using:</p>
  
  <pre><code>get-aduser -filter * -SearchBase "ou=Users,ou=xx,ou=xx,dc=xx,dc=local" | FT SamAccountName
  </code></pre>
  
  <p>and </p>
  
  <pre><code>Get-ADComputer -LDAPFilter "(name=compu*)" -searchbase "ou=xx,ou=xx,dc=xx,dc=local" | FT Name
  </code></pre>
  
  <p>These produce the exact details I need, both usernames and hostnames respectively.</p>
  
  <p>I can add a group to my username using:</p>
  
  <pre><code>Add-ADGroupMember -Identity "%GROUPNAME%" -Members MyUsername
  </code></pre>
  
  <p>I can't get a group to add to a computer as it errors:</p>
  
  <blockquote>
    <p>Add-ADGroupMember : Cannot find an object with identity: 'COMPUTERNAME' under: 'DC=XX,DC=LOCAL'. At line:1 char:1</p>
  </blockquote>
  
  <p>I even tried being clever by using:</p>
  
  <pre><code>$Comps=Get-ADComputer -LDAPFilter "(name=compu*)" -searchbase "ou=XX,ou=XX,dc=XX,dc=local" | FT Name
  
  Add-ADGroupMember -identity $Comps -MemberOf '%GROUPNAME%' 
  </code></pre>
  
  <p>but that just fails.</p>
  
  <p>Tried similar with users:</p>
  
  <pre><code>$Userlist=get-aduser -filter * -SearchBase "ou=XX,ou=XX,ou=XX,ou=XX,dc=XX,dc=local" | FT SamAccountName
  
  $Results = $Userlist | ForEach-Object {
   Add-ADGroupMember -identity %GROUPNAME% -members $_
  }
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>Add-ADGroupMember : Cannot bind parameter 'Members'. Cannot convert the
    "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" value of type
    "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" to type
    "Microsoft.ActiveDirectory.Management.ADPrincipal".<br>
    At line:4 char:105</p>
    
    <ul>
    <li>... %GROUPNAME% -members $_  </li>
    <li>~~  </li>
    <li>CategoryInfo          : InvalidArgument: (:) [Add-ADGroupMember], ParameterBindingException  </li>
    <li>FullyQualifiedErrorId :<br>
    CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember</li>
    </ul>
  </blockquote>
  
  <p>As I've copied and pasted many examples and tried to sanitise, the OU statement locations are correct.</p>
  
  <p>Currently I don't know if I should use <code>add-adgroupmember</code> or <code>add-adprinciplegroupmembership</code>, and I see I am making a fundamental error somewhere.</p>
  
  <p>I appreciate I could output hostnames or usernames to a txt and import from that but it seems an unnecessary step.</p>
  
  <p>Please help!</p>