Solved

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

Posted on 2014-11-12
8
289 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 26

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
 
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 26

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 26

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 26

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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…

919 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