Organized

This commit is contained in:
poslop
2024-09-20 12:33:47 -05:00
parent de1c912f9a
commit c3832e8c60
2 changed files with 26 additions and 1 deletions

View File

@@ -0,0 +1,27 @@
# Delete old profiles based off of the last login in AD
$daysInactive = 14 # Specify the number of days of inactivity
$inactiveDate = (Get-Date).AddDays(-$daysInactive)
$homeDirectoryPath = "C:\Users" # Path to user home directories
# Get all user profiles
Get-WmiObject Win32_UserProfile | ForEach-Object {
$profile = $_
$userFolder = $profile.LocalPath
$userName = $profile.LocalPath.Split('\')[-1] # Get the username from the folder path
# Get the last logon time for the local user
$userLogon = Get-WmiObject -Class Win32_NetworkLoginProfile -Filter "Name='RPBLUEJAYS\\$userName'" | Select-Object -Property LastLogon
if ($userLogon) {
$lastLogonDate = [Management.ManagementDateTimeConverter]::ToDateTime($userLogon.LastLogon)
# Check if the user's last local logon date is older than the specified inactive date
if ($lastLogonDate -lt $inactiveDate) {
Write-Host "Deleting user profile: $($profile.LocalPath) Last Logon: $lastLogonDate" -ForegroundColor Red
# Remove the user profile
$profile.Delete()
}
}
}