# Script: LimparPerfisTemporarios.ps1 # Necessita ser executado com permissões elevadas (Administrador/Sistema) $ProfileKeyPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" $UserProfiles = Get-ItemProperty -Path "$ProfileKeyPath\*" | Where-Object { $_.PSChildName -match 'S-1-5-21-\d+-\d+-\d+-\d+' } Write-Output "--- Iniciando a Verificação de Perfis para Exclusão ---" $DeletedCount = 0 foreach ($Profile in $UserProfiles) { $ProfilePath = $Profile.ProfileImagePath # 1. Verifica se o caminho é uma pasta de usuário válida (exclui perfis do sistema) if ($ProfilePath -like "C:\Users\*") { # Extrai o nome de usuário do caminho (por exemplo, "202usuario1234") $UserName = Split-Path -Leaf $ProfilePath # 2. Aplica o filtro: começa com "202" E comprimento > 11 if ($UserName.StartsWith("202") -and $UserName.Length -gt 11) { $ProfileSID = $Profile.PSChildName Write-Output "`n[DELETANDO] Perfil: $UserName (Caminho: $ProfilePath, SID: $ProfileSID)" # Tenta Excluir a Pasta try { Remove-Item -Path $ProfilePath -Recurse -Force -ErrorAction Stop Write-Output "Pasta excluída com sucesso." } catch { Write-Warning "AVISO: Falha ao excluir a pasta '$ProfilePath'. Pode estar em uso. Erro: $($_.Exception.Message)" } # Tenta Excluir a Chave de Registro try { Remove-Item -Path "$ProfileKeyPath\$ProfileSID" -Recurse -Force -ErrorAction Stop Write-Output "Chave de registro excluída com sucesso." $DeletedCount++ } catch { Write-Error "ERRO: Falha ao excluir a chave de registro para SID $ProfileSID." } } # else { # # Descomente para debug: # # Write-Output "Perfil '$UserName' não atende aos critérios." # } } } Write-Output "`n--- Verificação Concluída. Total de perfis excluídos: $DeletedCount ---" # O script termina.