title: Adding users to a group which aren't already member of it Indirect
body: <p>I'm having trouble with my pscode.</p>
  
  <pre><code>Import-Module ActiveDirectory
  </code></pre>
  
  <h1>Config</h1>
  
  <pre><code>$counter = 0
  $allready_members = 0
  $allready_members_file = "c:\temp\allready_members.txt"
  $new_members = 0
  $new_members_file = "c:\temp\new_members.txt"
  $groupname = "EU-GPO-CTXPVS.MinimumIdleSessionTime.Users"
  $fnc_accounts = @()
  $allready_member_accounts = @()
  </code></pre>
  
  <h1>Begin</h1>
  
  <pre><code>Write-Host "-----------------------------------------------------------------"
  Write-Host "- querying ActiveDirectory for FNC-accounts..."
  Write-Host "-----------------------------------------------------------------"
  
  Clear-Content $new_members_file
  Clear-Content $allready_members_file
  
  $fnc_accounts = Get-ADUser -Properties MemberOf, DisplayName, samAccountName, PasswordExpired -Filter  {(Enabled -eq "True") -and (PasswordNeverExpires -eq "True")} | ? { ($_.distinguishedname -like '*OU=FNC*') } | select Displayname, samAccountName, PasswordExpired 
  
  $allready_member_accounts = Get-ADGroupMember $groupname -Recursive | select samAccountName
  </code></pre>
  
  <h1>Main</h1>
  
  <pre><code>foreach($fncaccount in $fnc_accounts) {
  
      if (($allready_member_accounts -notcontains $fncaccount.samAccountName)){
          #Add-ADGroupMember -Identity $groupname -Members $fncaccount.sAMAccountName
          Write-Host "+ User "$fncaccount.SamAccountName" added to "$groupname
          #Add-Content -Path $new_members_file $fncaccount.samAccountName
          $new_members++
      } else {    
          Write-Host "- User "$fncaccount.SamAccountName" is already a member of "$groupname
          #Add-Content -Path $allready_members_file $fncaccount.samAccountName
          $allready_members++
      }
  
  $counter++
  
  }
  </code></pre>
  
  <h1>End</h1>
  
  <pre><code>Write-Host "-----------------------------------------------------------------" 
  Write-host "- # FNC count          : "$fnc_accounts.Count
  Write-Host "- # allready count     : "$allready_member_accounts.Count
  Write-host "- # allready counter   : "$allready_members
  Write-host "- # new members counter: "$new_members
  Write-host "- # items processed    : "$counter
  Write-Host "-----------------------------------------------------------------" 
  </code></pre>
  
  <p>My ps script doesn't seem to find the matches in my arrays. Any ideas ? I've used the counters to see if my check in foreach is correct.</p>