Difference between revisions of "Inactive AD accounts"

From UVOO Tech Wiki
Jump to navigation Jump to search
Line 1: Line 1:
 
# Get inactive accounts
 
# Get inactive accounts
 
```
 
```
# Import the Active Directory module
 
 
Import-Module ActiveDirectory
 
Import-Module ActiveDirectory
 
+
$ouPath = "OU=Some Accounts,DC=example,DC=com"
# Define the OU path
 
$ouPath = "OU=Example Foo Accounts,DC=example,DC=com"
 
 
 
# Define the date thresholds
 
$PasswordLastSetDateThreshold = Get-Date -Date "July 11, 2023"
 
$logonDateThreshold = Get-Date -Date "July 11, 2023"
 
 
 
# Get all user accounts in the specified OU that haven't had their passwords reset since the date threshold,
 
# whose SamAccountName starts with "svc-", have logged in after the logon date threshold, and are active
 
 
$usersNoPasswordReset = Get-ADUser -Filter {
 
$usersNoPasswordReset = Get-ADUser -Filter {
     PasswordLastSet -gt $PasswordLastSetDateThreshold -and
+
     PasswordLastSet -gt "7/11/2023 00:00:00 AM" -and
 +
    LastLogonDate -lt "7/11/2023 00:00:00 AM" -and
 
     SamAccountName -like "somesvc-*" -and
 
     SamAccountName -like "somesvc-*" -and
    LastLogonDate -lt $logonDateThreshold -and
 
 
     Enabled -eq $true
 
     Enabled -eq $true
} -SearchBase $ouPath -Properties PasswordLastSet, LastLogonDate, Enabled |  
+
} -SearchBase $ouPath -Properties PasswordLastSet, LastLogonDate, Enabled |
 
     Select-Object Name, SamAccountName, PasswordLastSet, LastLogonDate, Enabled
 
     Select-Object Name, SamAccountName, PasswordLastSet, LastLogonDate, Enabled
 
# Output the list of users
 
 
$usersNoPasswordReset | Format-Table -AutoSize
 
$usersNoPasswordReset | Format-Table -AutoSize
 
```
 
```

Revision as of 16:29, 15 August 2024

Get inactive accounts

Import-Module ActiveDirectory
$ouPath = "OU=Some Accounts,DC=example,DC=com"
$usersNoPasswordReset = Get-ADUser -Filter {
    PasswordLastSet -gt "7/11/2023 00:00:00 AM" -and
    LastLogonDate -lt "7/11/2023 00:00:00 AM" -and
    SamAccountName -like "somesvc-*" -and
    Enabled -eq $true
} -SearchBase $ouPath -Properties PasswordLastSet, LastLogonDate, Enabled |
    Select-Object Name, SamAccountName, PasswordLastSet, LastLogonDate, Enabled
$usersNoPasswordReset | Format-Table -AutoSize