title: PowerShell - add an exclusion into Remove-ADGroupMember command?
body: <p>When somebody leaves my organization, we remove all AD group memberships apart from the <code>PrimaryGroup</code> which is <code>Domain Users</code>. We often process these in batches, so pull the affected usernames from a CSV file.</p>
  <p>I have the following code, and while it does the job of deleting all group memberships, I get an error for each user:</p>
  <blockquote>
  <p>The user cannot be removed from a group because the group is currently the user's primary group</p>
  </blockquote>
  <p>Whilst it does the job, how can I &quot;clean up&quot; the process to avoid this message each time?  Is there a way to exclude <code>Domain Users</code> from the groups it removes the user from, or should I do this another way?</p>
  <pre><code>$users = Import-Csv &quot;c:\temp\leavers.csv&quot;
  
  foreach ($user in $users) {
  
  Get-ADPrincipalGroupMembership -identity $user.username | foreach {Remove-ADGroupMember $_ -Members $user.username -Confirm:$false}
  
  }
  </code></pre>