Solved

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

Posted on 2014-11-12
8
295 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 27

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
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.

 
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 27

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 27

Accepted Solution

by:
Dan McFadden earned 500 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 27

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

16 Experts available now in Live!

Get 1:1 Help Now