Advertisement
posLop

logs edelete profile

Sep 20th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. $daysInactive = 7 # 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. $userProfiles = Get-WmiObject Win32_UserProfile
  7.  
  8. # Iterate over each user profile
  9. foreach ($profile in $userProfiles) {
  10. $userFolder = $profile.LocalPath
  11. $userName = $userFolder.Split('\')[-1] # Get the username from the folder path
  12.  
  13. # Get the last logon time from the Event Logs
  14. $lastLogon = Get-EventLog -LogName Security -InstanceId 4624 |
  15. Where-Object { $_.ReplacementStrings[5] -eq $userName } |
  16. Select-Object -First 1 -Property TimeGenerated
  17.  
  18. if ($lastLogon) {
  19. $lastLogonDate = $lastLogon.TimeGenerated
  20.  
  21. # Check if the last logon date is older than the specified inactive date
  22. if ($lastLogonDate -lt $inactiveDate) {
  23. Write-Host "Deleting user profile: $($profile.LocalPath) Last Local Logon: $lastLogonDate" -ForegroundColor Red
  24.  
  25. # Remove the user profile
  26. $profile.Delete()
  27. }
  28. }
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement