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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.