Automating Python Uninstallation with PowerShell

In this article I will discuss about how we can remotely uninstall all versions of python and from all user profile.

Python, a versatile and widely-used programming language, has become an integral part of many development environments. However, ensuring the security of your systems demands not only the use of up-to-date software but also the efficient removal of outdated versions. In this blog post, we delve into a PowerShell script designed to automate the uninstallation of all Python versions across multiple user profiles. This automation is especially valuable in environments where client management tools like SCCM or Tanium are employed to maintain software consistency across numerous machines.

 

The Need for Uninstallation Automation:

Managing multiple Python installations across various user profiles manually can be a cumbersome and time-consuming task. Moreover, outdated Python versions might be associated with vulnerabilities, such as Denial of Service (DoS), emphasizing the critical need to uninstall them efficiently. As identified by Nessus ID 156198 and Qualys ID 375320, these vulnerabilities underscore the importance of keeping Python installations up-to-date for robust security.

 

PowerShell Script to Uninstall Python Versions

 

# Function to uninstall Python using the registry uninstall string

function UninstallPython($uninstallString) {

    $uninstallCommand = “$uninstallString /quiet”

    Write-Host “Uninstalling Python…”

    Start-Process -Wait -FilePath cmd.exe -ArgumentList “/C”, $uninstallCommand -WindowStyle Hidden

}

 

# Get a list of installed Python versions from the registry for all users

$pythonVersions = Get-ChildItem -Path “HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall”, “HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall” -Recurse -ErrorAction SilentlyContinue |

Get-ItemProperty |

Where-Object { $_.DisplayName -like ‘Python*’ }

 

# Uninstall each Python version

foreach ($version in $pythonVersions) {

    $uninstallString = $version.UninstallString

    UninstallPython $uninstallString

}

 

# Remove Python installation directories for all users

$pythonDirectories = @(

    “${Env:ProgramFiles}\Python”,

    “${Env:ProgramFiles(x86)}\Python”,

    “${Env:LOCALAPPDATA}\Programs\Python”

)

 

foreach ($directory in $pythonDirectories) {

    $directories = Get-ChildItem -Path $directory -Directory -Recurse -ErrorAction SilentlyContinue

    if ($directories) {

        Write-Host “Removing Python directories…”

        foreach ($dir in $directories) {

            Write-Host “Removing $($dir.FullName)…”

            Remove-Item -Path $dir.FullName -Recurse -Force

        }

    }

}

 

Understanding the Script:

 

1.    UninstallPython Function:

The script defines a function, UninstallPython, responsible for executing the uninstallation process using the registry uninstall string with a quiet parameter. This ensures a silent and streamlined uninstallation.

 

Retrieving Python Versions from Registry:

The script fetches a list of installed Python versions from both machine-wide (HKLM) and user-specific (HKCU) registry locations, focusing on entries with display names matching ‘Python*’.

 

Iterative Uninstallation:

The script then iterates through each Python version, invoking the UninstallPython function to ensure a thorough uninstallation process.

 

Removing Python Directories:

Subsequently, it identifies and removes Python installation directories for all users, ensuring a clean uninstallation process by eliminating leftover files and directories.

 

Conclusion:

Automating the uninstallation of Python versions using PowerShell provides a streamlined and efficient approach to maintaining a secure computing environment. The script presented in this blog post not only simplifies the removal of outdated Python installations but also ensures that associated directories are thoroughly cleaned. In environments where managing multiple installations is a complex task, automation becomes a crucial component of maintaining software consistency and security.

 

As organizations strive to fortify their defenses against potential vulnerabilities, incorporating automated processes into their cybersecurity strategy is essential. By regularly updating and, when necessary, uninstalling outdated software, businesses and individuals can significantly reduce the risk of exploitation. This PowerShell script serves as a valuable tool in achieving these objectives, offering a proactive and efficient solution for enhancing the security of systems relying on the Python programming language.

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top