Hi Glenn Maxwell,
Here is the compiled script with the modifications you requested. I have tested this in my lab environment, and the behavior seems to match your requirement to exclude any accounts ending with .priv. I hope you find this helpful!
$Days = 7
$Cutoff = (Get-Date).AddDays(-$Days)
# Modified filter to exclude .priv accounts directly at the source
$users = Get-ADUser -Filter {
Enabled -eq $false -and
whenChanged -ge $Cutoff -and
SamAccountName -notlike "*.priv" -and
UserPrincipalName -notlike "*.priv"
} -Properties co, department, whenChanged, title |
Select-Object Name, title, SamAccountName,
@{n="DisabledDate";e={$_.whenChanged.Date}}
# Daily count summary
$dailyCounts = $users | Group-Object DisabledDate |
Sort-Object Name -Descending |
Select-Object @{n="Date";e={$_.Name}}, @{n="Count";e={$_.Count}}
# Output to Console
Write-Host "`n=== Disabled Users (Last 7 Days) ===" -ForegroundColor Cyan
$users | Sort-Object DisabledDate -Descending | Sort-Object samaccountname | Format-Table
Write-Host "`n=== Daily Disable Counts ===" -ForegroundColor Yellow
$dailyCounts | Sort-Object date
# Export results to a CSV file (Please update the destination path as needed)
$users | Export-Csv -Path "C:\temp\DisabledUsers.csv" -NoTypeInformation -Encoding UTF8
If this helps resolve your problem, please consider hitting "Accept Answer" so other users facing this failure can easily find the solution!
Thank you so much!