Solved

Export Computer Accounts from Certain OUs, using Powershell ?

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

932 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

14 Experts available now in Live!

Get 1:1 Help Now