Solved

Powershell to list all users from domains in forest

Posted on 2014-11-05
5
2,561 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
  • 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

Promote certifications in your email signature

Has your company recently won an award or achieved a certification? They'll no doubt want to show it off. Email signature images used to promote certifications & awards can instantly establish credibility with a recipient and provide you with numerous benefits.

Question has a verified solution.

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

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

911 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

25 Experts available now in Live!

Get 1:1 Help Now