Skip to main content

Powershell: Get-ADUser

# Users last logon more than 180 days

PS C:\Windows\system32>Import-Module ActiveDirectory

PS C:\Windows\system32>$today = Get-Date -Format yyyyMMdd

PS C:\Windows\system32>$date = (Get-Date).AddDays(-180)

PS C:\Windows\system32> Get-ADUser -Filter {LastLogonDate -lt $date} -Properties * | Select Name,SamAccountName,Office,LastLogonDate | Sort-Object Office | Export-Csv -Path C:\Audit\last-logon-180-days-$today.csv -NoTypeInformation