Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $daysInactive = 7 # Specify the number of days of inactivity
- $inactiveDate = (Get-Date).AddDays(-$daysInactive)
- $homeDirectoryPath = "C:\Users" # Path to user home directories
- # Get all user profiles
- $userProfiles = Get-WmiObject Win32_UserProfile
- # Iterate over each user profile
- foreach ($profile in $userProfiles) {
- $userFolder = $profile.LocalPath
- $userName = $userFolder.Split('\')[-1] # Get the username from the folder path
- # Get the last logon time from the Event Logs
- $lastLogon = Get-EventLog -LogName Security -InstanceId 4624 |
- Where-Object { $_.ReplacementStrings[5] -eq $userName } |
- Select-Object -First 1 -Property TimeGenerated
- if ($lastLogon) {
- $lastLogonDate = $lastLogon.TimeGenerated
- # Check if the last logon date is older than the specified inactive date
- if ($lastLogonDate -lt $inactiveDate) {
- Write-Host "Deleting user profile: $($profile.LocalPath) Last Local Logon: $lastLogonDate" -ForegroundColor Red
- # Remove the user profile
- $profile.Delete()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement