Solved

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

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

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

744 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

15 Experts available now in Live!

Get 1:1 Help Now