Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need list of member from OU, but not the child OU's.

Posted on 2014-11-12
8
Medium Priority
?
314 Views
Last Modified: 2014-11-14
I have the syntax below to get all the members of the win7 OU. But there are other OU's in the Win7 OU and I do not want the member from them, just the members of the win7 ou.   How can I modify my script to give me the just the members under win7 and not the other OU's?


Get-ADComputer -SearchBase 'OU=win7,OU=Workstations,OU=Machines,DC=global,DC=company,DC=com' -Filter '*' | Select -Exp Name > g:\win7.csv
0
Comment
Question by:rdefino
  • 4
  • 2
  • 2
8 Comments
 
LVL 5

Expert Comment

by:A Karelin
ID: 40439206
Try add
SearchScope
Specifies the scope of an Active Directory search. Possible values for this parameter are:
Base or 0
OneLevel or 1
Subtree or 2
0
 
LVL 29

Expert Comment

by:Dan McFadden
ID: 40439504
I would use the following:

$SearchBase = "OU=Your,OU=Structure,OU=Goes,OU=Here,DC=YourDomainName,DC=Extension"
$SearchScopeControl = "OneLevel"
$OutputFile = "MyOutputFile.csv"

Get-ADComputer -Filter * -SearchBase $SearchBase -SearchScope $SearchScopeControl | Export-Csv $OutputFile -noTypeInformation -UseCulture

Open in new window


This will give you a little more flexibility if you need to reuse the script often.  And the Export-Csv command will give you a CSV that you can open directly in Excel without having to import the data.

Dan
0
 

Author Comment

by:rdefino
ID: 40439919
Hi Dan,

This looks to have worked fine, but one thing.

How can I get the output just to show the members names, instead of all of this:
DistinguishedName      DNSHostName      Enabled      Name      ObjectClass      ObjectGUID      SamAccountName      SID      UserPrincipalName



thanks
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 5

Expert Comment

by:A Karelin
ID: 40439951
Get-ADComputer -Filter * -SearchBase $SearchBase -SearchScope $SearchScopeControl | select Name | Export-Csv $OutputFile -noTypeInformation -UseCulture

Instead "Name" You can write "DNSHostName" or "DistinguishedName, DNSHostName, Name"
0
 
LVL 29

Expert Comment

by:Dan McFadden
ID: 40440042
Oh!  I pasted a wrong command line into the script... oops.

I would use this:

$SearchBase = "OU=Your,OU=Structure,OU=Goes,OU=Here,DC=YourDomainName,DC=Extension"
$SearchScopeControl = "OneLevel"
$OutputFile = "MyOutputFile.csv"

Get-ADComputer -Filter * -SearchBase $SearchBase -SearchScope $SearchScopeControl -Properties * | select Name, DNSHostName, DistinguishedName | Export-Csv $OutputFile -noTypeInformation -UseCulture

Open in new window


Dan
0
 

Author Comment

by:rdefino
ID: 40440283
cool, 1 last question. When outputting just the name of the members. In the csv file the column starts with "name", since I'm searching just for that. Is there a way to have the csv file not show the word "name" and just output the members?
0
 
LVL 29

Accepted Solution

by:
Dan McFadden earned 2000 total points
ID: 40442085
If you mean that you do not want the header row, the you can use this line instead:

(Get-ADComputer -Filter * -SearchBase $SearchBase -SearchScope $SearchScopeControl -Properties * | select Name, DNSHostName, DistinguishedName | ConvertTo-Csv -noTypeInformation -UseCulture) | Select -skip 1 | Set-Content -Path $OutputFile

Open in new window


Just replace the original Get-ADComputer command with the command above and let it run.

Dan
0
 
LVL 29

Expert Comment

by:Dan McFadden
ID: 40442090
If you wan fewer columns in the output, the you can remove whatever field names you want, after the "select" statement.  Adjust the highlighted section:

(Get-ADComputer -Filter * -SearchBase $SearchBase -SearchScope $SearchScopeControl -Properties * | select Name, DNSHostName, DistinguishedName | ConvertTo-Csv -noTypeInformation -UseCulture) | Select -skip 1 | Set-Content -Path $OutputFile

Dan
0

Featured Post

Independent Software Vendors: 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

Here's a look at newsworthy articles and community happenings during the last month.
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

972 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