Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell to list all users from domains in forest

Posted on 2014-11-05
5
Medium Priority
?
3,941 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

916 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