content.php

Powershell Script – All Users who have never logged on

A short powershell script to show you all users on your domain who have never logged on.

Sometimes, if your organization’s automations aren’t tight, you end up creating AD Accounts for new employees who’s hire get’s rescinded before they actually start. In which case you can end up with users in your AD who have never logged on. Good practice is to automate a Check that periodically checks your AD to see if any of these ghost user accounts exist.

#only show me accounts created more than 30 days ago
$createdtime = (Get-Date).AddDays(-(30))

Get-ADUser -Filter {(lastlogontimestamp -notlike "*") -and (enabled -eq $true) -and (whencreated -lt $createdtime)}
Select Name, DistinguishedName, sAMAccountName |
Export-Csv "C:\Location\FileName.csv" -NoTypeInformation -Encoding UTF8

Leave a Reply

Your email address will not be published. Required fields are marked *