Kanuc
Kanuc

Reputation: 35

Uninstall all Python versions via Intune and install only the latest version

The goal is to have only the latest version of Python (3.13.1) installed after the process, with all previous versions removed. Note: I work in the customer's environment and have to deploy the new version to more than 500 devices.

I’m using a PowerShell script (included at the end of this post) to:

  1. Remove all existing versions of Python (folders, registry keys, etc.).
  2. Install only the latest version of Python (3.13.1).

When I run the script locally, launching PowerShell as Administrator, everything works as expected: all Python installations are removed, and only version 3.13.1 remains.

However, when I execute the same script via Microsoft Intune (as a Win32 App with system privileges), some Python versions are not removed. For example, the Python Launcher is deleted, but the main versions (e.g., 3.9, 3.10) remain.

Even though no errors appear in Intune logs, running the command python --version still shows an older version.

Questions:

  1. Why does the uninstallation work correctly locally but fail to completely remove Python when executed under the NT AUTHORITY\SYSTEM context?
  2. How can I remove Python installations stored in the HKCU registry or local folders (e.g., D:\Python)? Could it be that Intune’s system account doesn’t have access to user registry keys (HKCU) or certain directories?
  3. What’s the best strategy to ensure a complete removal of any Python version (including user-based installations) and cleanly install Python 3.13.1 via Intune?

Script

Write-Host "Removing all Python versions and related traces..."

# Remove Python via registry
Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall, HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall | ForEach-Object {
    $DisplayName = $_.GetValue("DisplayName")
    if ($DisplayName -like "*Python*") {
        $UninstallString = $_.GetValue("UninstallString")
        if ($UninstallString) {
            Write-Host "Uninstalling: $DisplayName"
            Start-Process -FilePath "cmd.exe" -ArgumentList "/c $UninstallString" -Wait -NoNewWindow
        }
    }
}

# Remove residual Python folders
$PythonPaths = @(
    "$env:ProgramFiles\Python*",
    "$env:ProgramFiles(x86)\Python*",
    "$env:LocalAppData\Programs\Python",
    "$env:AppData\Python",
    "$env:LocalAppData\Python"
)
foreach ($Path in $PythonPaths) {
    if (Test-Path -Path $Path) {
        Write-Host "Removing directory: $Path"
        Remove-Item -Path $Path -Recurse -Force -ErrorAction SilentlyContinue
    }
}

Write-Host "Complete removal of Python executed."

# Python installation
$PythonInstallerUrl = "https://www.python.org/ftp/python/3.13.1/python-3.13.1.exe"
$InstallerPath = "$env:TEMP\python_installer.exe"

Write-Host "Downloading Python installer..."
Invoke-WebRequest -Uri $PythonInstallerUrl -OutFile $InstallerPath

Write-Host "Installing Python..."
Start-Process -FilePath $InstallerPath -ArgumentList "/quiet InstallAllUsers=1 PrependPath=1" -Wait

Write-Host "Removing the installer..."
Remove-Item -Path $InstallerPath -Force

Write-Host "Python successfully installed."

Upvotes: 0

Views: 102

Answers (1)

ppel123
ppel123

Reputation: 93

You could try to run a script like this one to remove everything related to Python from you endpoints.
You have to alter it a bit in order to include a for loop to search the registry for every Python installation, and perform the silent uninstallation. Let me know if you need any further assistance.

Upvotes: 0

Related Questions