View difference between Paste ID: kM4RcRQ5 and iqNm4sTf
SHOW: | | - or go back to the newest paste.
1
$daysInactive = 14  # Specify the number of days of inactivity
2
$inactiveDate = (Get-Date).AddDays(-$daysInactive)
3
$homeDirectoryPath = "C:\Users"  # Path to user home directories
4
5
# Get all user profiles
6
Get-WmiObject Win32_UserProfile | ForEach-Object {
7
    $profile = $_
8
    $userFolder = $profile.LocalPath
9
    $userName = $profile.LocalPath.Split('\')[-1]  # Get the username from the folder path
10
	
11
    # Get the last logon time for the local user
12
    $userLogon = Get-WmiObject -Class Win32_NetworkLoginProfile -Filter "Name='RPBLUEJAYS\\$userName'" | Select-Object -Property LastLogon
13
14
    if ($userLogon) {
15
        $lastLogonDate = [Management.ManagementDateTimeConverter]::ToDateTime($userLogon.LastLogon)
16
17
        # Check if the user's last local logon date is older than the specified inactive date
18
        if ($lastLogonDate -lt $inactiveDate) {
19
            Write-Host "Deleting user profile: $($profile.LocalPath) Last Logon: $lastLogonDate" -ForegroundColor Red
20
21
            # Remove the user profile
22
            $profile.Delete()
23
        }
24
    }
25
}