Solved

Powershell to list all users from domains in forest

Posted on 2014-11-05
5
3,394 Views
Last Modified: 2014-11-11
I am looking for some help with a script that will list all user accounts from a specific OU for all domains under a forest.  The accounts exist in an OU called Accounts under the root of each domain.

If someone has a script for this or can help write something it would be appreciated.
0
Comment
Question by:Uptime Legal Systems
[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
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40424652
Here you go. If you want additional info, let me know.

Import-Module ActiveDirectory
(Get-ADForest).domains | % {
Get-ADUser -filter * -SearchBase "OU=Accounts,$((Get-ADDomain).DistinguishedName)" | Select Name, sAMAccountName | FT
}

Open in new window


This will save each domain to a CSV
Import-Module ActiveDirectory
(Get-ADForest).domains | % {
Get-ADUser -filter * -SearchBase "OU=Accounts,$((Get-ADDomain).DistinguishedName)" | Select Name, sAMAccountName | Export-CSV C:\$_ User Accounts.csv -nti
}

Open in new window

0
 
LVL 6

Author Comment

by:Uptime Legal Systems
ID: 40424748
Hey Joshua,

This looks like what it will do what I need but when I run it I get the following error:

Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "User" to type "System.Char". Error: "String must be exactly one character long."
At C:\Users\Administrator\Desktop\ForestAudit.ps1:3 char:133
+ ... port-CSV C:\$_ User Accounts.csv -nti
+                    ~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand
 
Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "User" to type "System.Char". Error: "String must be exactly one character long."
At C:\Users\Administrator\Desktop\ForestAudit.ps1:3 char:133
+ ... port-CSV C:\$_ User Accounts.csv -nti
+                    ~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand
 
Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "User" to type "System.Char". Error: "String must be exactly one character long."
At C:\Users\Administrator\Desktop\ForestAudit.ps1:3 char:133
+ ... port-CSV C:\$_ User Accounts.csv -nti
+                    ~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand

Open in new window


Any ideas?
0
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 500 total points
ID: 40424772
My mistake. when there are spaces in the file name you have to surround in quotations. Here you go!

Import-Module ActiveDirectory
(Get-ADForest).domains | % {
Get-ADUser -filter * -SearchBase "OU=Accounts,$((Get-ADDomain).DistinguishedName)" | Select Name, sAMAccountName | Export-CSV "C:\$_ User Accounts.csv" -nti
}

Open in new window

0
 
LVL 6

Author Closing Comment

by:Uptime Legal Systems
ID: 40424856
Thanks, that's good!
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40436008
Here is a correction to the above script after a follow up. I realized that it was pulling the same distinguished name for each domain in the forest.

Import-Module ActiveDirectory
(Get-ADForest).domains | % {
Get-ADUser -filter * -SearchBase "OU=Accounts,$((Get-ADDomain -Server $_).distinguishedname)" -Server $_ | Select Name,sAMAccountName | Export-CSV "C:\$_ User Accounts.csv" -nti
}

Open in new window

0

Featured Post

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Configuring Remote Assistance for use with SCCM
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..
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦
Suggested Courses

622 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