Find and export users not logged in - Active Directory

This is a little PowerShell script that finds and exports a list of users not logged in, in a number of days, from your Active Directory.

This is the content of the script.

Just copy and paste it into your own PowerShell Script, or use the script attached to this tip.

You can run this from a workstation on which you are logged in to the Active Directory. You don't need to be Domain Admin to run this report.

Extract the files to a local folder, not to the desktop.

Software you need:

Download the Quest ActiveRoles Management Shell for Active Directory from
#This script allows you to export a list of users not logged in the last 90 days. You can change the days as you wish.
#By N. Poulsen 2012

#Run this script from a workstation logged in to the Active Directory which you want to pull the date from.
#Place and run it from a local folder (not desktop).

#First thing to do is to download and install the Quest ActiveRoles Management Shell for Active Directory from "".
#Just run the "ActiveRolesManagmentShell_x86.msi" file and you're good to go.

#Before running this script be sure to run this command in a commandprompt or using the script, RemoteSigned.bat, which will do this for you.
#Command to run: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -Command "&{set-executionpolicy RemoteSigned -Force}"

#You need to make changes to this obtion, to get it working for your own environment:
# $DIR=''
#Change it to match your directory

#Add the Quest Active Roles
echo "Installing Quest ActiveRoles Management Shell for Active Directory"
Add-PSSnapin Quest.ActiveRoles.ADManagement

Start-Sleep -s 5

#Clear screen

#Set the date variable. Set's it to the day of today
echo "Today is $Date"
echo ""

#Set the Days variable. Specify how many days the users haven't logged in.
echo "Days users not logged in = $Days"
echo ""

#Set the directory source variable. Specify your search directory from the Active Directory.
echo "Search directory = $DIR"
echo ""

#The Get-QADUser cmdlet now sersch through the directory for users not logged in the last 90 days and exports it to a csv file called users90.csv.
#The file will be palced in the folder you run this script from. The users logonname and displayname will be posted in the file.

echo "Exporting list of users not logged in the last $Days days"
echo "Please wait, this could take some time to complete"
echo ""

Get-QADUser -SearchRoot $DIR -sizeLimit 0 | where {
$_.lastlogontimestamp -and
(($Date-$_.lastlogontimestamp).days -gt $Days)
} | Select-Object logonName, DisplayName | export-csv users90.csv

function Wait-KeyPress($prompt='Export complete press any key') {
Write-Host $prompt

do {
Start-Sleep -milliseconds 100
} until ($Host.UI.RawUI.KeyAvailable)

All information should be in the script, otherwise please let me know.


How To-Best Practice
Comment List