This short blog post will show you how you can find orphaned user accounts in Active Directory that may no longer be in use. Let’s go.
Here is an example screen of the code.
Copy the lines below to PowerShell ISE or Visual Studio Code and run it. The Code will retrieve all user accounts that have not been logged on to the domain for 365 days.
Note that you may need to edit line 2 to suit your needs. Enter the days you want to calculate back.
# Calculate back x days from today $date = (Get-Date).AddDays(-365) # Retrieve users Get-ADUser ` -Filter 'lastlogondate -le $date' ` -Properties LastLogonDate | Select-Object Name,LastLogonDate