Solved

Export Computer Accounts from Certain OUs, using Powershell ?

Posted on 2014-11-16
3
70 Views
Last Modified: 2015-01-23
Does anyone have a script available that can extract computer accounts from OUs and not from others ? What I would like to do is to run a PowerShell script to extract computer accounts, which I have done.  This pulled out all computer accounts, but what I would like to do, is to extract computer accounts from all OUs apart from the Computer \ Test \ Dev OUs.
0
Comment
Question by:CaussyR
  • 2
3 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 40446896
If the number of computers you're excluding is relatively small the simplest way would be to do something like this:
Get-ADComputer (Filters) |
  Where-Object {
    $_.DistinguishedName -notmatch 'CN=Computers'  -and 
    $_.DistinguishedName -notmatch 'OU=Test' -and
    $_.DistinguishedName -notmatch 'OU=Dev'
  }

Open in new window

Exactly how this fits in depends on how you're executing the query in the first place. However, the filtering principals will still apply.

Filtering after the event like this is not the most efficient method, so if you have a very large number of computers a deeper look is required.

Chris
0
 

Author Comment

by:CaussyR
ID: 40446987
Thanks Chris, but if there are sub-OUs how do I stop the search running through each sub OU ?

Test > Test1 > Test2
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 40447162
Given that we're qualifying the distinguishedName you can do this based on all or part of the path.

For example, if you wanted to exclude a root OU called Test you could do this in the Where-Object statement:
$_.DistinguishedName -match 'OU=Test,DC=yourdomain,DC=com$'

Open in new window

It's a regular expression matching anything at the end of the string so we look for yourdomain.com/Test in effect.

You can look for nested OUs in much the same way:
$_.DistinguishedName -match 'OU=Test2,OU=Test1,OU=Test'

Open in new window

Is that what you mean?

Chris
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
A procedure for exporting installed hotfix details of remote computers using powershell
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

747 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

12 Experts available now in Live!

Get 1:1 Help Now