Finding old AD user accounts with powershell

The following is a modified script that I have used to pulling old/inactive user accounts out of AD.

# Activate the required module in PS for this script to work.
import-module activedirectory

# 90 is the number of days from today since the last logon.
$then = (Get-Date).AddDays(-90)

# -Searchbase is OU where user accounts are kept. *Excluded Sub OU* is required if a sub OU Needs to be excluded from the search.
Get-ADUser -Property Name,lastLogonDate -Filter {Enabled -eq $true -and lastLogonDate -ltĀ  $then} -SearchBase “OU=AD Users,DC=Domain,DC=com” | ? {($_.DistinguishedName -notlike “*Excluded Sub OU*”)} | Select-Object -property Name,lastLogonDate | sort Name,lastLogonDate | Export-Csv C:\SCRIPTS\Logs\User_Records.csv -Delimiter “;” -notype