File size: 2,090 Bytes
04da94d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<#

.SYNOPSIS

    Remove um ou mais caminhos do PATH do utilizador de forma persistente e segura.

    Garante a integridade do PATH sem os limites do comando 'setx'.



.EXAMPLE

    .\Remove-Path.ps1 "C:\Caminho\Para\Remover"

#>

param (
    [Parameter(Mandatory=$true, HelpMessage="Caminho a remover do PATH")]
    [string[]]$PathsToRemove
)

# 1) Obter o PATH atual do registo (User level)
$currentPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if (-not $currentPath) {
    Write-Error "Não foi possível obter o PATH do utilizador ou o PATH está vazio."
    exit
}

$pathList = $currentPath -split ";" | Where-Object { $_ -ne "" }
$originalCount = $pathList.Count
$newPathList = @()

# Criar lista de caminhos a remover limpos
$cleanRemovals = $PathsToRemove | ForEach-Object { $_.Trim().Trim('"').TrimEnd('\') }

foreach ($p in $pathList) {
    $cleanP = $p.Trim().TrimEnd('\')
    
    # Se o caminho atual não estiver na lista de remoção, mantém-se
    $match = $false
    foreach ($rem in $cleanRemovals) {
        if ($cleanP -ieq $rem) {
            $match = $true
            break
        }
    }
    
    if (-not $match) {
        $newPathList += $p
    } else {
        Write-Host "🗑️ A remover: $p" -ForegroundColor Yellow
    }
}

$removedCount = $originalCount - $newPathList.Count

if ($removedCount -gt 0) {
    # Juntar novamente
    $updatedPath = $newPathList -join ";"
    
    # Gravar permanentemente no Registo (User)
    [Environment]::SetEnvironmentVariable("PATH", $updatedPath, "User")
    
    # Atualizar a sessão atual
    $env:PATH = [Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" + $updatedPath
    
    Write-Host "`n✨ Sucesso! $removedCount caminho(s) removido(s) do PATH permanentemente." -ForegroundColor Green
    Write-Host "📢 Nota: Novas consolas refletirão a mudança. A sessão atual foi atualizada."
} else {
    Write-Host "`nNada a remover. Os caminhos indicados não foram encontrados no PATH do utilizador." -ForegroundColor Gray
}