Avatar of digitalhitman00
 asked on

AD Powershell query for Users and Groups

I have a script that is running two commands, 1. get the AD Group and 2. List the users who are not in that AD group.  This works fine. What I need to do is expand my AD Groups variable to hold multiple Groups and 2. List the users who do not fall in any of those AD Groups.

1. $groups = Get-ADGroup -Filter { Name -like "*groupname*"}     <-- that will hold multiple groups in the $groups variable.
2. Get-ADUser -filter {memberof -notlike $groups}  <--This doesnt work as is, but the logic is to list the users that do not fall in any of those groups.

whats the correct way, efficient way of doing this?  I have tons of groups setup in a specific naming convention where I can filter with a wildcard and pull all the groups I need.  I just need to compare each user to see if they fall in any one of those groups. If they do not fall into any of the groups then list the user.
PowershellActive Directory

Avatar of undefined
Last Comment

8/22/2022 - Mon
Rajitha Chimmani

How about the following approach instead of taking the groups separately and then checking for each user.

Get-ADUser -properties memberof | where {$_.memberof -notcontains "*groupname*"}

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck