Solved

Export Computer Accounts from Certain OUs, using Powershell ?

Posted on 2014-11-16
3
79 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

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

776 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