diff --git a/scripts/DeleteOldProfilesManual.ps1 b/scripts/DeleteOldProfilesManual.ps1 new file mode 100644 index 0000000..15fb282 --- /dev/null +++ b/scripts/DeleteOldProfilesManual.ps1 @@ -0,0 +1,92 @@ +#Requires -RunAsAdministrator + +$UserName = "*" +$ExcludedUserNames = @("Administrator", "Default") +$InactiveDays = 14 +$ComputerName = $env:computername + + +Set-strictmode -version latest + +ForEach ($computer in $ComputerName) +{ + $profilesFound = 0 + + Try { + $profiles = Get-WmiObject -Class Win32_UserProfile -Computer $computer -EnableAllPrivileges + } 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 = [System.Management.ManagementDateTimeConverter]::ToDateTime($profile.LastUseTime) + $isExcluded = $False + + #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) {continue} + + If($InactiveDays -ne [uint32]::MaxValue -And $profileUnusedDays -le $InactiveDays){continue} + + $profilesFound ++ + + If ($profilesFound -gt 1) {Write-Host "`n"} + Write-Host "Start deleting profile ""$account"" on computer ""$computer"" ..." -ForegroundColor Green + Write-Host "Account SID: $sid" + 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 + } + + If ($PSCmdlet.ShouldProcess($account)) { + 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 found on $ComputerName with Name $UserName" + } +}