diff --git a/scripts/DeleteOldProfilesDirectory.ps1 b/scripts/DeleteOldProfilesDirectory.ps1 deleted file mode 100644 index 1dc296f..0000000 --- a/scripts/DeleteOldProfilesDirectory.ps1 +++ /dev/null @@ -1,23 +0,0 @@ -# Delete old profiles based on the home users directory last modification date - -$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 - - # Check if the profile folder exists and get the last modification date - if (Test-Path $userFolder) { - $lastWriteTime = (Get-Item $userFolder).LastWriteTime - - if ($lastWriteTime -lt $inactiveDate) { - Write-Host "Deleting user profile: $($profile.LocalPath) Last Modified: $lastWriteTime" -ForegroundColor Red - - # Remove the user profile - $profile.Delete() - } - } -} diff --git a/scripts/DeleteOldProfilesManual.ps1 b/scripts/DeleteOldProfilesManual.ps1 deleted file mode 100644 index d77bc10..0000000 --- a/scripts/DeleteOldProfilesManual.ps1 +++ /dev/null @@ -1,104 +0,0 @@ -#Requires -RunAsAdministrator -[cmdletbinding(ConfirmImpact = 'High', SupportsShouldProcess=$True)] - -$UserName = "*" -$ExcludedUserNames = @("Administrator", "Default Profile") -$InactiveDays = 14 -$ComputerName = $env:computername - - - -ForEach ($computer in $ComputerName) -{ - $profilesFound = 0 - Try { - $profiles = Get-CimInstance -Class Win32_UserProfile - } Catch { - Write-Warning "Failed to retreive user profiles on $ComputerName" - Exit - } - - - ForEach ($profile in $profiles) { - $sid = New-Object System.Security.Principal.SecurityIdentifier($profile.SID) - $account = $sid.Translate([System.Security.Principal.NTAccount]) - $accountDomain = $account.value.split("\")[0] - $accountName = $account.value.split("\")[1] - $profilePath = $profile.LocalPath - $loaded = $profile.Loaded - $lastUseTime = $profile.LastUseTime - $isExcluded = $False - $special = $profile.Special - - - If ($special) {continue} - - #Calculation of the login date - $lastLoginDate = $null - If ($accountDomain.ToUpper() -eq $computer.ToUpper()) {$lastLoginDate = [datetime]([ADSI]"WinNT://$computer/$accountName").LastLogin[0]} - - #Calculation of the unused days of the profile - $profileUnusedDays=0 - If (-Not $loaded){ - If($lastLoginDate -eq $null){ $profileUnusedDays = (New-TimeSpan -Start $lastUseTime -End (Get-Date)).Days } - Else{$profileUnusedDays = (New-TimeSpan -Start $lastLoginDate -End (Get-Date)).Days} - } - - If($accountName.ToLower() -Eq $UserName.ToLower() -Or - ($UserName.Contains("*") -And $accountName.ToLower() -Like $UserName.ToLower())) { - - ForEach ($eun in $ExcludedUserNames) { - If($eun -ne [string]::Empty -And -Not $eun.Contains("*") -And ($accountName.ToLower() -eq $eun.ToLower())){ - $isExcluded = $True - break - } - If($eun -ne [string]::Empty -And $eun.Contains("*") -And ($accountName.ToLower() -Like $eun.ToLower())){ - $isExcluded = $True - break - } - } - - If($isExcluded) {Write-Host "Profile $accountName was excluded!" continue} - - If($InactiveDays -ne [uint32]::MaxValue -And $profileUnusedDays -le $InactiveDays){ - - Write-Host "`nSkipping ""$account"" as it is recently used." -ForegroundColor Blue - Write-Host "Account SID: $sid" - Write-Host "Special system service user: $special" - Write-Host "Profile Path: $profilePath" - Write-Host "Loaded : $loaded" - Write-Host "Last use time: $lastUseTime" - If ($lastLoginDate -ne $null) { Write-Host "Last login: $lastLoginDate" } - Write-Host "Profile unused days: $profileUnusedDays" - continue} - - $profilesFound ++ - - If ($profilesFound -gt 1) {Write-Host "`n"} - Write-Host "`nStart deleting profile ""$account"" on computer ""$computer"" ..." -ForegroundColor Red - Write-Host "Account SID: $sid" - Write-Host "Special system service user: $special" - Write-Host "Profile Path: $profilePath" - Write-Host "Loaded : $loaded" - Write-Host "Last use time: $lastUseTime" - If ($lastLoginDate -ne $null) { Write-Host "Last login: $lastLoginDate" } - Write-Host "Profile unused days: $profileUnusedDays" - - If ($loaded) { - Write-Warning "Cannot delete profile because is in use" - Continue - } - - Try { - $profile.Delete() - Write-Host "Profile deleted successfully" -ForegroundColor Green - } Catch { - Write-Host "Error during delete the profile" -ForegroundColor Red - } - } - } - - If($profilesFound -eq 0){ - Write-Warning "No profiles to delete" - } -} diff --git a/scripts/DeleteOldProfilesNetProfile.ps1 b/scripts/DeleteOldProfilesNetProfile.ps1 deleted file mode 100644 index bb5ca6d..0000000 --- a/scripts/DeleteOldProfilesNetProfile.ps1 +++ /dev/null @@ -1,38 +0,0 @@ -# Delete old profiles based off of the last login in AD - -$daysInactive = 14 # Specify the number of days of inactivity -$homeDirectoryPath = "C:\Users" # Path to user home directories -$domainName = "local" # Domain name that the pc is on -$excludedProfiles = @("Default User", "administrator") - - -$inactiveDate = (Get-Date).AddDays(-$daysInactive) -$profiles = Get-WmiObject Win32_UserProfile - -# Get all user profiles -:Loop foreach ($profile in $profiles) { - $userFolder = $profile.LocalPath - $userName = $profile.LocalPath.Split('\')[-1] # Get the username from the folder path - $nameIsExcluded = $false - - foreach ($name in $excludedProfiles) { - if ($item -eq $userName) { - continue Loop - } - } - - # Get the last logon time for the local user - $userLogon = Get-WmiObject -Class Win32_NetworkLoginProfile -Filter "Name='$domainName\\$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() - } - } -}