Functions/Uninstall-Python.ps1

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
function Uninstall-Python([string]$Version){
$Null = @(
    $parts = $Version.split('.')
    # Check if the specified version is installed, and if the short version was specified expand it to the full version.
    $installed = $true
    if ($parts.count -lt 3){
        $fullVersion = Convert-ShortVersionToFullVersion $Version
        if (!$fullVersion){
            $installed = $false
        }
        $Version = $fullVersion
    }
    else {
        $installed = Test-FullVersionIsInstalled $Version
    }

    if (!$installed){
        throw "Python version $Version is not installed. Please use 'Install-Python' to install it. Or call 'Get-InstalledPythonVersions' to see a list of all installed versions."
    }

    $installRoot = Get-PythonInstallRoot
    $shortVersion = Convert-FullVersionToShortVersion $Version
    $installDirName = "python$shortVersion"
    $installLocation = "$installRoot/$installDirName"
    $installerEXEPath = Get-PythonInstaller $Version

    Write-Host "Uninstalling python version $shortVersion..."
    # Wait for executable to complete so we can delete it when we are done
    $result = Start-Process $installerEXEPath -ArgumentList "/uninstall /passive" -NoNewWindow -Wait -PassThru
    if (!($result.ExitCode -eq 0)) {
        throw "Python installer crashed silently while uninstalling."
    }

    Write-Host "Removing the installer executable at $installerEXEPath..."
    Remove-Item -Path $installerEXEPath
    # The folder where python was installed is removed by the installer when it finishes.
)
}