?
Solved

Export Computer Accounts from Certain OUs, using Powershell ?

Posted on 2014-11-16
3
Medium Priority
?
85 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 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 71

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

Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
A small collection of useful tips and tricks for Windows 10 users that I decided to write as a result of recent questions that were asked and answered at Experts Exchange. Two short video tutorials included. Enjoy..
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

752 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