Solved

Get-aduser excluding certain OU's

Posted on 2016-11-07
3
26 Views
Last Modified: 2016-11-07
I have staff and student OU's in each of our parent OU's that represent the building they are in.   I am searching the entire AD for a display name and occasionally one of the students will have the same name as a staff member.  I would like to omit any student names from the results.  The staff would be in an OU named staff and the students would be in an OU named students.  There are many different staff and student OU's in our AD.  I need to keep any students out of the results. Can you help with this?

$adduser = "john Doe"
$schoollist = "Kennedy"
$d = Get-Aduser -Filter {displayname -eq $adduser} -Property displayname
add-ADGroupMember $schoollist -Members $d
0
Comment
Question by:Roccat
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41877234
$adduser = "john Doe"
$schoollist = "Kennedy"
$d = Get-Aduser -Filter {displayName -eq $adduser} -Property displayname | ? {$_.DistinguishedName -notlike '*,OU=Students,*'}
Add-ADGroupMember $schoollist -Members $d

Open in new window

0
 

Author Comment

by:Roccat
ID: 41877239
Thank you! What does the question mark mean?
 ? {$_.DistinguishedName -notlike '*,OU=Students,*'}
0
 
LVL 83

Expert Comment

by:oBdA
ID: 41877242
It's short for "Where-Object" (and "%" is an alias for "ForEach-Object").
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now