Solved

Powershell to list all users from domains in forest

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

Don't miss ATEN at NAB Show April 24-27!

Visit ATEN at NAB Show to learn how our "Seamlessly Entertaining" solutions deliver fast, precise video streaming without delays for the broadcasting and media environment. ATEN will showcase its 16x16 Modular Matrix Switch (VM1600) and KVM Over IP Solution (KE6900 series).

Question has a verified solution.

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

This article demonstrates probably the easiest way to configure domain-wide tier isolation within Active Directory. If you do not know tier isolation read https://technet.microsoft.com/en-us/windows-server-docs/security/securing-privileged-access/s…
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

679 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