Public/Uninstall-CpmfUipsPackCommandLineTool.ps1

function Uninstall-CpmfUipsPackCommandLineTool {
<#
.SYNOPSIS
    Removes the uipcli and .NET runtime tool directories and cleans DOTNET_ROOT
    and the associated PATH entry from the user environment registry.
 
.PARAMETER CliVersion
    UiPath CLI version to remove. Defaults to 25.10.15.
 
.PARAMETER UipcliPath
    Optional absolute path to an existing uipcli.exe. Used to infer tool family and root path.
 
.PARAMETER ToolBase
    Root directory used during installation. Defaults to %LOCALAPPDATA%\cpmf\tools.
 
.PARAMETER ToolBasePath
    Canonical tool root directory. Same as -ToolBase; kept for the shared path-var naming convention.
#>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [string]$CliVersion = '25.10.15',
        [string]$UipcliPath,
        [Alias('ToolBase')]
        [string]$ToolBasePath = (Join-Path $env:LOCALAPPDATA 'cpmf\tools')
    )

    Set-StrictMode -Version Latest
    $ErrorActionPreference = 'Stop'

    $p = Get-CpmfUipsToolPaths -CliVersion $CliVersion -UipcliPath $UipcliPath -ToolBase $ToolBasePath

    Write-Verbose "[Uninstall] Removing uipcli $CliVersion ..."
    if (Test-Path $p.CliToolDir) {
        if ($PSCmdlet.ShouldProcess($p.CliToolDir, 'Remove uipcli tool directory')) {
            Remove-Item $p.CliToolDir -Recurse -Force
            Write-Verbose "[Uninstall] Removed $($p.CliToolDir)"
        }
    } else {
        Write-Verbose "[Uninstall] $($p.CliToolDir) not found — skipping"
    }

    Write-Verbose "[Uninstall] Removing .NET runtime ..."
    if (Test-Path $p.DotnetDir) {
        if ($PSCmdlet.ShouldProcess($p.DotnetDir, 'Remove .NET runtime directory')) {
            Remove-Item $p.DotnetDir -Recurse -Force
            Write-Verbose "[Uninstall] Removed $($p.DotnetDir)"
        }
    } else {
        Write-Verbose "[Uninstall] $($p.DotnetDir) not found — skipping"
    }

    Write-Verbose "[Uninstall] Cleaning user environment variables ..."

    # DOTNET_ROOT is only set by the classic (23.x/.NET 6) install path
    if ($p.Generation -eq 'classic') {
        if ($PSCmdlet.ShouldProcess('DOTNET_ROOT (User)', 'Clear environment variable')) {
            [Environment]::SetEnvironmentVariable('DOTNET_ROOT', $null, 'User')
            Write-Verbose "[Uninstall] Cleared DOTNET_ROOT"
        }
    }

    if ($PSCmdlet.ShouldProcess("PATH (User): $($p.DotnetToken)", 'Remove entry')) {
        if (Remove-FromUserPath $p.DotnetToken) {
            Write-Verbose "[Uninstall] Removed $($p.DotnetToken) from user PATH"
        } else {
            Write-Verbose "[Uninstall] $($p.DotnetToken) not found in user PATH — skipping"
        }
    }

    Write-Verbose "[Uninstall] Done"
}