Export Last Logon times in Active Directory using Powershell

 

 

Ok so this is how you export Last Logon times in Active Directory using Powershell

What you will need:

After installing the Quest Scripts on your Domain Controller do the following:

Changes that you need to do to the script:

  1. $OU – Enter your domain or Organizational Unit path
  2. $NumberDays – Enter the number of days last logon that you want exported – in this case we want to export all users that have not logged on for the past 60 days

Copy the following script and save it as export-last-logon.ps1 using notepad.

$OU=”DC=domain,DC=com”
$NumberDays=60
$COMPAREDATE=GET-DATE

GET-QADUSER -SizeLimit 0 -SearchRoot $OU | where { $_.LastLogon.AddDays($NumberDays) -gt $CURRENTDATE } | where { $_.lastlogontimestamp -le (get-date).adddays(-$NumberDays) } |  select Name, ParentContainer, Department, Office, Description, LastLogonTimeStamp, LastLogon, AccountIsDisabled | Export-Csv c:\Users_Not_Logged_in_past_60_days.csv -noTypeInformation

Now copy the .ps1 file to your c:/ on your domain controller.

Next we will open Windows Powershell Modules located on Start > All Programs > Administrative Tools > Windows Powershell Modules

In Powershell type in cd\ and hit the return or enter key

Now type in .\export-last-logon.ps1 and hit the return on enter key

After the script has completed you will see that it created a new .csv file called Users_Not_Logged_in_past_60_days.csv. You can open this file using excel or any text editor and view your results.

If this was helpful please click on the STAR rating and give me a thumbs up, you can also comment below 🙂

Have a good one!! Cheers!