Export users (CSV) – but exclude users that are members of a specific group

Welcome Forums General PowerShell Q&A Export users (CSV) – but exclude users that are members of a specific group

Viewing 1 reply thread
  • Author
    Posts
    • #230392
      Participant
      Topics: 1
      Replies: 0
      Points: -8
      Rank: Member

      I have a PowerShell script that I’ve been using to spit out a CSV of all users in a specific OU, but I want to exclude users that are a member of a specific security group. Basically, all the users exist in the same OU otherwise I could choose which OU to pull from.

      The only unique factor is the group they are a member of. What could I use to exclude these users?

      Would adding this to the ‘get-aduser’ line work?

      Here is a section of the script

    • #230413
      Participant
      Topics: 1
      Replies: 55
      Points: 526
      Helping Hand
      Rank: Major Contributor

      Hello aparbatani23,

      On the line #1 you are getting users and attributes. By default Get-ADUser gets only 10 basic attributes and you are adding 3 more (mail,country,department).

      So every AD user object in  your case contains 13 attributes:

      1. DistinguishedName
      2. Enabled
      3. GivenName
      4. Name
      5. ObjectClass
      6. ObjectGUID
      7. SamAccountName
      8. SID
      9. Surname
      10. UserPrincipalName
      11. Mail
      12. Country
      13. Department

      And then on line #8 you are simply renaming country attribute to group. It does not contains Group Membership. ADUser object property which contains groups is memberOf.

      You can use notcontains operator but keep in mind that memberof  contains collection of Distinguished  Names of AD Groups that user is part of.

      In order for notcontains to work you need to use distinguished name of the group in your filter:

       

      Hope that helps.

       

Viewing 1 reply thread
  • The topic ‘Export users (CSV) – but exclude users that are members of a specific group’ is closed to new replies.