Avatar of CaussyR
CaussyR

asked on 

Export Computer Accounts from Certain OUs, using Powershell ?

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.
PowershellWindows OS

Avatar of undefined
Last Comment
Chris Dent
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of CaussyR
CaussyR

ASKER

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

Test > Test1 > Test2
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Windows OS
Windows OS

This topic area includes legacy versions of Windows prior to Windows 2000: Windows 3/3.1, Windows 95 and Windows 98, plus any other Windows-related versions including Windows Mobile.

129K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo