Solved

Get-aduser excluding certain OU's

Posted on 2016-11-07
3
18 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

14 Experts available now in Live!

Get 1:1 Help Now