Assistance on powershell query

Glenn Maxwell 14,206 Reputation points
2026-07-28T05:57:33.1266667+00:00

Hi All,

I am using the query below to retrieve disabled user accounts from Active Directory and export the results to a CSV file.

I would like to exclude any accounts where either the SamAccountName or the UserPrincipalName (UPN) ends with .priv.

For example:

  • user1.priv → Do not export (whether .priv is in the SamAccountName or UPN)
  • user2.priv → Do not export (whether .priv is in the SamAccountName or UPN)
  • user1 → Export
  • user2 → Export

Could someone please advise how to modify the below query so that any account with .priv in either the SamAccountName or the UserPrincipalName (UPN) is excluded from the CSV export?

$Days = 7
$Cutoff = (Get-Date).AddDays(-$Days)
$users = Get-ADUser -Filter {
    Enabled -eq $false -and whenChanged -ge $Cutoff } -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
Write-Host "`n=== Disabled Users (Last 7 Days) ===" -ForegroundColor Cyan
$users | Sort-Object DisabledDate -Descending | Sort samaccountname | ft
Write-Host "`n=== Daily Disable Counts ===" -ForegroundColor Yellow
$dailyCounts | sort date
Windows for business | Windows Server | Directory services | Active Directory
0 comments No comments

Answer accepted by question author
Steven Nguyen (WICLOUD CORPORATION) 415 Reputation points Microsoft External Staff Moderator
2026-07-28T07:54:46.4166667+00:00

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!

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Oldest
  1. JimmySalian-2011 45,896 Reputation points Volunteer Moderator
    2026-07-28T06:43:42.1033333+00:00

    Hi Glenn,

    I thnk you will need a filter -notlike "."in the* he script.

    UserPrincipalName -notlike ".priv" -and SamAccountName -notlike ".riv"

    Add this in the script and it should do the task.

    Hope this helps.

    JS

    ==

    Please Accept the answer if the information helped you. This will help us and others in the community as well.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.