Solved

Powershell to list all users from domains in forest

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

Join & Write a Comment

Resolve DNS query failed errors for Exchange
A procedure for exporting installed hotfix details of remote computers using powershell
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now