How to get a report on Users and workstations in Active directory

To whom it may concern

I need to do an Active Directory Cleanup for one of our clients.  The Manager is not always sure what accounts are used and what PC's is still active.  Is there a way either via Command Prompt or Powershell script that I can run that will show me all the user accounts in AD and when last they logged onto the server.  If we can see the workstations in AD and when last they made connectivity, that would be a bonus.

Basically I need to remove the unused accounts and workstations that is no longer used.  The report will help me and the manager make the correct decision and not deleting a working account.

Specs of Server on Site:
Intel Xeon CPU E5-2609 v3 @ 1.90GHz (2 processors)
64GB Ram
1TB C:
2TB D:
Windows Server 2012 R2 Standard 64 bit

Please let me know if you require further information.

Kind Regards
Lourens van DykIT Technical ConsultantAsked:
Who is Participating?
Tom CieslikIT EngineerCommented:
You can run save this script to let say inactive.ps1 file

import-module activedirectory  
$domain = "domain.local"  
$DaysInactive = 90  
$time = (Get-Date).Adddays(-($DaysInactive)) 
# Get all AD User with lastLogonTimestamp less than our time and set to enable 
Get-ADUser -Filter {LastLogonTimeStamp -lt $time -and enabled -eq $true} -Properties LastLogonTimeStamp | 
# Output Name and lastLogonTimestamp into CSV  
select-object Name,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp).ToString('yyyy-MM-dd_hh:mm:ss')}} | export-csv c:\temp\OLD_Users.csv -notypeinformation

Open in new window

and replace domain.local with your domain name.
Run it on domain controller and you'll get list of last logon users saved in c:\temp\old_Users.csv file.

Make sure c:\temp folder is present on server or change path in script.
Naveen SharmaCommented:
Copy and run the following script to generate last logon reports on the command screen.

Import-Module ActiveDirectory

function Get-LastLogonEvents
$dcs = Get-ADDomainController -Filter {Name -like "*"}
$users = Get-ADUser -Filter *
$time = 0

foreach($user in $users)
foreach($dc in $dcs)
$hostname = $dc.HostName
$currentUser = Get-ADUser $user.SamAccountName | Get-ADObject -Server $hostname -Properties lastLogon
if($currentUser.LastLogon -gt $time) 
$time = $currentUser.LastLogon
$dt = [DateTime]::FromFileTime($time)
Write-Host $currentUser "last logged on at:" $dt
$time = 0

Open in new window

To cleanup Active Directory, you can use Lepide's Active Directory Cleanup solution. This tool provides reports inactive users and computers, users who never logged on etc. Also, you can manage inactive accounts by moving, disabling, deleting them or resetting their passwords
Lourens van DykIT Technical ConsultantAuthor Commented:
Tom's solution and PS script definitely worked.  Tested on Server and got the result.  I would award every post here points if I could as the other solutions will also work.  I just needed notepad + + to save the file to *.ps1.  Thank you all for assistance.  Will try out the other powershell scripts as well.
Tom CieslikIT EngineerCommented:
You are very welcome :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.