Advertisement
posLop

profile scripts

Sep 18th, 2024 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 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. 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. Write-Host $userFolder
  12.  
  13. # Get the last logon time for the local user
  14. $userLogon = Get-WmiObject -Class Win32_NetworkLoginProfile -Filter "Name='$userName'" | Select-Object -Property LastLogon
  15.  
  16. Write-Host $userLogon
  17.  
  18. if ($userLogon) {
  19. $lastLogonDate = [Management.ManagementDateTimeConverter]::ToDateTime($userLogon.LastLogon)
  20.  
  21. # Check if the user's last local 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