Solved

Get-aduser excluding certain OU's

Posted on 2016-11-07
3
61 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 85

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 85

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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