Querying and exporting Accounts from Multiple OUs

I used the following script to export User accounts beginning with "z" from a domain. I need to export user accounts from multiple OUs within a domain in series.  For example:

import-module activedirectory
Get-ADUser -Filter {name -like "z*"} -Properties name, samaccountname | select name, samaccountname
Get-WmiObject -Namespace "Root\CIMv2" -Class "Win32_Service" | Select-Object Name, DisplayName, StartName

#Get-Process | Out-File c:\scripts\wservices.txt -width 120

OUs:

ABC
XYZ

How can I modify the example script above to perform the export?

Lipotech
lipotechSys EngAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NinjaStyle82Systems AdministratorCommented:
probably nest it inside of a foreach ou loop like so:
foreach($unit in Get-ADOrganizationalUnit -filter *){get-aduser -filter * -SearchBase $unit}

Open in new window

0
lipotechSys EngAuthor Commented:
How do I specify a specific OU?  I don't want everything.  I just need a specific set of OUs.  Once again, for example:

ABC
XYZ
.....

Lipotech.
0
NinjaStyle82Systems AdministratorCommented:
you could use the distinguished names. for example you could build an array of the OUs you want to search, lets say
$oulist = @("OU=ABC,OU=Accounts,DC=Contoso,DC=Com","OU=XYZ,OU=Accounts,DC=Contoso,DC=Com")
foreach ($ou in $oulist)
{
get-aduser -filter * -SearchBase $ou
}

Open in new window

0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

lipotechSys EngAuthor Commented:
Okay. Your recommendation looks like it will work. My PowerShell skills are limited. I have a top level OU that nest the target I want to query as follows:

ABC
      DEF
      GHI
      JKL
      ........

With that noted, how should I arrange the syntax.  Would the following code segment work?

-----------------------------------------------------------------------------------------------

import-module activedirectory
 Get-ADUser -Filter {name -like "z*"} -Properties name, samaccountname | select name, samaccountname
 Get-WmiObject -Namespace "Root\CIMv2" -Class "Win32_Service" | Select-Object Name, DisplayName, StartName

$oulist = @("OU=ABC,OU=Accounts,DC=Contoso,DC=Com","OU=XYZ,OU=Accounts,DC=Contoso,DC=Com")
foreach ($ou in $oulist)
{
get-aduser -filter * -SearchBase $ou
}
 #Get-Process | Out-File c:\scripts\wservices.txt -width 120

---------------------------------------------------------------------------------------------------

Thanks,

Lipotech
0
NinjaStyle82Systems AdministratorCommented:
Not exactly sure what your script is doing, if you just want to search each OU in the parent OU for Z names, then output the names you could use this. (Change the OU Distinguished name to match the correct OU in your domain)
foreach ($ou in (Get-ADOrganizationalUnit -filter * | Where-Object {$_.DistinguishedName -like "*,OU=ABC,DC=your,DC=domain,DC=name"}))
{
Get-ADUser -Filter {name -like "z*"} -Properties name, samaccountname -SearchBase $ou | Add-Member -NotePropertyName "User OU" -NotePropertyValue $ou -PassThru -Force | select "User OU", name, samaccountname
}

Open in new window

1
lipotechSys EngAuthor Commented:
I am using the following script to extract all user accounts from ABC.Corp domains.  The script works without issue.  How do I modify this script to segregate based on subtrees within the ABC.Corp domain?  For example:

ABC.Corp  (Parent Domain)
       DEF  (Subtree  OU)
       GHI  (Subtree  OU)
       JKL    (Subtree  OU)
       ........


Import-Module Activedirectory

$path = 'C:\temp'
$list = 'C:\temp\ADUsers.csv'

if (!(test-path -path $path)) {New-Item -ItemType Directory -path $path}


$ADUserParams=@{
'Searchbase' = 'DC=ABC,DC=corp'
'Searchscope'= 'Subtree'
'Filter' = '*'
'Properties' = 'DisplayName', 'SamAccountName', 'UserPrincipalName', 'Title', 'CanonicalName', 'EMailAddress'
}

get-aduser @ADUserParams | Select-Object -Property 'DisplayName', 'SamAccountName', 'UserPrincipalName', 'Title', 'CanonicalName', 'EMailAddress' | Sort-Object 'DisplayName'| export-csv $list -NoTypeInformation

$wshell = New-Object -comobject wscript.shell
$wshell.popup("User List Exported to $list",0,"AD User Export Complete")
0
lipotechSys EngAuthor Commented:
Frank,

I think I see what you are saying now with the 'foreach' cmdlet.  I never use it before. Not sure where to place it in the script.

Lipotech
0
NinjaStyle82Systems AdministratorCommented:
Do you want a separate CSV for each OU?
You could use the same script i posted earlier and just pipe the line inside the foreach into an export-csv with an ou variable in the path, and you will have a separate csv for each OU.

foreach ($ou in (Get-ADOrganizationalUnit -filter * | Where-Object {$_.DistinguishedName -like "*,OU=ABC,DC=your,DC=domain,DC=name"}))
{
if (Get-ADUser -Filter {name -like "a*"} -Properties name, samaccountname -SearchBase $ou | select name, samaccountname | Export-CSV -Path $exportpath -Append -NoTypeInformation)
{
$exportpath = "$env:USERPROFILE\Desktop\"+$ou.name+".csv"
}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lipotechSys EngAuthor Commented:
Excellent Support!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.