Solved

Powershell script to list AD users

Posted on 2013-06-21
8
519 Views
Last Modified: 2013-06-26
I need a Powershell script to list all users in AD with their OU location and basic account info. Currently I only need to know if the password is set to never expire but you can add other useful columns. The output shout go to txt or csv file. Thanks!
0
Comment
Question by:MisUszatek
[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
8 Comments
 
LVL 69

Expert Comment

by:Qlemo
ID: 39266427
Is this Windows 2008R2 (or later)? If so, we can use the ActiveDirectory module and its Get-AD* cmdlets. If not, I recommend to get Quest ActiveRoles (free), and use their Get-QAD* cmdlets.
0
 

Author Comment

by:MisUszatek
ID: 39266436
It is 2008R2. I don't know much about PS so I need the exact script...
0
 
LVL 24

Expert Comment

by:Sandeshdubey
ID: 39266444
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 69

Expert Comment

by:Qlemo
ID: 39266493
Import-Module ActiveDirectory
Get-ADUser -Filter * -Properties *

Open in new window

will list all info for all AD objects (groups and users). To get a specific property, like the Password Never Expires setting, put that instead of the * after -Properties:
Get-ADUser -Filter * -Properties PasswordNeverExpires | Select Name, Pass*

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39266499
It is always a good idea to ask PS for help:
  Get-Help Get-ADUser -detailed
or
 Get-Help Get-AD*
and to get the available properties
 Get-ADUser ExistingADUserHere -Properties *| get-member
(replace the user name, of course).
0
 

Author Comment

by:MisUszatek
ID: 39266671
@qlemo
What do I need to do to have a column that shows an OU where the user is located?
0
 

Expert Comment

by:willie0-360
ID: 39267085
I think you can get it by doing this on PowerShell V3 (might work on PowerShell v3):

Get-ADUser -Identity lastnameFirstInitial -Properties DistinguishedName

To get all the properties of a user:

Get-ADUser -Identity lastnameFirstInitial -Properties *

where lastnameFirstInitial is the username the user uses to log on to the Network.

If you are running Windows 2008 R2, you will need to import the AD  module by running:

import-module servermanager

Add-WindowsFeature -Name "RSAT-AD-PowerShell" -IncludeAllSubFeature



Thanks.
--Willie
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 350 total points
ID: 39268091
The OU is not available directly, only as part of the complete path.
CanonicalName is something like domain.com/TestOU/SubOU/UserName
DistinguishedName is then CN=UserName,OU=SubOU,OU=TestOU,DC=domain,dc=com
I will use CanonicalName, and add that to the command I showed already:
Get-ADUser -Filter * -Properties PasswordNeverExpires, CanonicalName | Select Name, Pass*, @{n='OU'; e={($_.CanonicalName -split '/')[-2]}}

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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