Solved

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

Posted on 2014-11-12
8
300 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

A brief introduction to what I consider to be the best editor for PowerShell.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

790 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