#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 "http://www.quest.com/activeroles-server/".
#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='novell.com/users/cool' 
#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
cls

#Set the date variable. Set's it to the day of today
$Date=get-date
echo "28/01/2014"
echo ""

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

#Set the directory source variable. Specify your search directory from the Active Directory.
$DIR='ud1.utility/Accounts'
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)	
            $Host.UI.RawUI.FlushInputBuffer()
       }
       Wait-KeyPress