title: Adding Computer to Multiple Active Directory Groups During Task Sequence Using Powershell
body: <p>I'm trying to set up my task sequence for SCCM to automatically add laptops to 3 Active Directory groups. I've set up a step to do this and am calling upon a Powershell script to do so. The script should be run as a network admin so I don't believe I'm having any issues with authorization however I am super new to Powershell so I believe my issue is with my syntax. My script is simple enough and all of the resources I look at seem to overcomplicate Powershell for what I need to do. Here is my script:</p>
  
  <pre><code>ADD-ADGroupMember "GroupOne" -members "$env:computername$"
  ADD-ADGroupMember "GroupTwo" -members "$env:computername$"
  ADD-ADGroupMember "GroupThree" -members "$env:computername$"
  </code></pre>
  
  <p>The $env:computername is supposed to automatically gather the computer's name which is established earlier in the task sequence and the $ following it is required to add using powershell, I've found.</p>
  
  <p>Any help on this would be very much appreciated.</p>
  
  <p>EDIT: I've got it working finally, below is the code I've found and used for one of the powershell scripts -</p>
  
  <pre><code> $ComputerName = gc env:computername
  
   $isMember = new-object DirectoryServices.DirectorySearcher([ADSI]"")
   $ismember.filter = “(&amp;(objectClass=computer)(sAMAccountName=$Computername$)(memberof=CN=&lt;CN NAME&gt;,OU=&lt;OU NAME&gt;,DC=&lt;DC NAME&gt;,DC=&lt;DC NAME&gt;))”
   $isMemberResult = $isMember.FindOne()
  
   If ($isMemberResult) {exit}
  
   else
  {
     $searcher = new-object DirectoryServices.DirectorySearcher([ADSI]"")
     $searcher.filter = “(&amp;(objectClass=computer)(sAMAccountName= $Computername$))”
     $FoundComputer = $searcher.FindOne()
     $P = $FoundComputer | select path
     $ComputerPath = $p.path
     $GroupPath = "LDAP://CN=&lt;CN NAME&gt;,OU=&lt;OU NAME&gt;,DC=&lt;DC NAME&gt;,DC=&lt;DC NAME&gt;"
     $Group = [ADSI]"$GroupPath"
     $Group.Add("$ComputerPath")
     $Group.SetInfo()
  }
  </code></pre>