Tag: ad


Get Users based on LastLogOnTimeStamp x number of days


# ==============================================================================================
# NAME: Get active users that have not signes in for x amount of day
# 
# AUTHOR: Vincent Christiansen, vincent@sameie.com
# DATE  : 16/12/2019
# 
# COMMENT: This script will prompt you for what OU you want the members listed (OUs - distinguishedName), how many days you want it to list from and export them to a txt file
#          If you change the MD directory (bellow), don't forget to change the $location too. Good luck
#
# ==============================================================================================

MD C:\tmp\Scripts -ErrorAction SilentlyContinue

    Write-Host
    Write-host "From what OU (distinguishedName) do you want to list the users? :" -ForegroundColor Yellow -Nonewline
    $OU = Read-Host 

    Write-host  "What do you want to call the file? :" -ForegroundColor Yellow -NoNewLine         
    $File = Read-Host

    Write-host  "How many days? :" -ForegroundColor Yellow -NoNewLine         
    $Days = Read-Host

$tmp = "$OU.tmp"
$location = "c:\tmp\Scripts\"
$output = "$location\$tmp.txt"



Get-ADUser -Filter {Enabled -eq $TRUE} -SearchBase $OU -Properties Name,SamAccountName,LastLogonDate | Where {($_.LastLogonDate -lt (Get-Date).AddDays(-$days)) -and ($_.LastLogonDate -ne $NULL)}| Sort | Select Name,SamAccountName,LastLogonDate | Out-File "$location\$File.txt"