Reputation: 89
I would like to insert a condition to check the AD user account expires date, how to implement it?
After selection, if the AD user account have fetch a value will expire in next day, then export the csv file.
Otherwise, ignore it and exit the program directly.
Thanks.
$Range = ((Get-Date).AddDays(1)).Date
Get-AdUser -Filter {AccountExpirationDate -eq $Range} –Properties AccountExpirationDate | Select-Object -Property SamAccountName, Name, AccountExpirationDate |
Export-CSV -Path "C:\Temp\Upcoming Retired User\Report_$((Get-Date).ToString("yyyyMMdd")).txt" -NoTypeInformation
Upvotes: 0
Views: 1164
Reputation: 61148
You are not actually using a Range of dates, but most of all, when you use -eq
, you are checking if there are accounts that will expire tomorrow, exactly at midnight.
I don't think this is what you want to do and instead want to know which accounts are to become expired on or before the reference date.
Try
$refDate = (Get-Date).AddDays(1).Date # tomorrow at midnight
# try to find expired accounts
$users = Get-AdUser -Filter "AccountExpirationDate -lt $refDate" –Properties AccountExpirationDate -ErrorAction SilentlyContinue
if ($users) {
$fileOut = Join-Path -Path 'C:\Temp\Upcoming Retired User' -ChildPath ('Report_{0:yyyyMMdd}.csv' -f (Get-Date))
$users | Select-Object -Property SamAccountName, Name, AccountExpirationDate |
Export-Csv -Path $fileOut -NoTypeInformation
}
else {
Write-Host 'No expired accounts found'
}
Upvotes: 2