Please help me out… I would like to get Accounts alredy expired and accounts that will expires in 100 days. It is getting the the table below, but no information. if I remove “-and (Search-ADAccount -AccountExpired -UsersOnly | Where-Object {$_.Enabled})” will show me account will exprire in 100, but does not show accounts already expired.
SamAccountName Mail Department Company Title Enabled Created AccountExpirationDate Description CanonicalName
cls
Import-Module ActiveDirectory
$OU=“OU=OtherUsers,OU=adm,DC=adm,DC=com”
$report = $null
$table = $null
$date = Get-Date -uformat “%d/%B/%Y” # Date format
$startDate = Get-Date
$endDate = $startDate.AddDays(100)
$total = (Get-ADUser -filter {AccountExpirationDate -gt $startDate -and AccountExpirationDate -lt $endDate} -SearchBase $OU).count # Total accounts on OU
$domain = (Get-ADDomain).Forest # Domain name
#–USER LIST------------------------------------------------#
$prop = @(‘SamAccountName’,‘Mail’,‘Department’,‘Company’,‘Title’,‘Enabled’,‘Created’,‘AccountExpirationDate’,‘Description’,‘CanonicalName’,‘Manager’)
$users = @(Get-ADUser -filter {AccountExpirationDate -gt $startDate -and AccountExpirationDate -lt $endDate} -SearchBase $OU -Properties $prop)-and (Search-ADAccount -AccountExpired -UsersOnly | Where-Object {$_.Enabled})
$result = @($users | Select-Object SamAccountName, Mail, Department, Company, Title, Enabled, Created, AccountExpirationDate, Description, CanonicalName, @{Name=‘Manager’;Expression={(Get-ADUser $_.Manager).sAMAccountName}})
Sort by user account A-Z
$result = $result | Sort “SamAccountName”
This line will not show script running.
$result | ft -auto