# Script Name: PyGo.ps1 # Description: This script checks if Python 3 is already installed on Windows 10/11. # If not, it downloads and installs the latest version, it also supports uninstalling Python if the -Uninstall flag is provided. # # Author: Jan Gebser # Date: 12.11.24 # Version: 1.3.2 # Contact: j.gebser@webservice.digital <# WORKING ON SOME MORE IMPROVEMENTS AND FEATURES - also to fix the following issue below: Get-ExecutionPolicy -List Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine ''' PS C:\Users\WDAGUtilityAccount\Desktop> .\PyGo.ps1 .\PyGo.ps1 : File C:\Users\WDAGUtilityAccount\Desktop\PyGo.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:1 + .\PyGo.ps1 + ~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess PS C:\Users\WDAGUtilityAccount\Desktop> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine PS C:\Users\WDAGUtilityAccount\Desktop> .\PyGo.ps1 Python is not installed. Proceeding with installation... Downloading Python installer from https://www.python.org/ftp/python/3.13.0/python-3.13.0-amd64.exe... Download complete. Installing Python... Installation complete. Cleaning up... Cleanup complete. Verifying installation... python : The term 'python' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Users\WDAGUtilityAccount\Desktop\PyGo.ps1:102 char:22 + $pythonVersion = python --version + ~~~~~~ + CategoryInfo : ObjectNotFound: (python:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException Python installation and verification complete. Installed version: Installation Date: 11/12/2024 10:56:27 Operating System Version: 2004 PS C:\Users\WDAGUtilityAccount\Desktop> .\PyGo.ps1 Python is not installed. Proceeding with installation... Downloading Python installer from https://www.python.org/ftp/python/3.13.0/python-3.13.0-amd64.exe... PS C:\Users\WDAGUtilityAccount\Desktop> ''' #> param ( [switch]$Uninstall ) # Function to check if Python is installed function Check-PythonInstalled { try { $pythonVersion = python --version 2>&1 if ($pythonVersion -match "Python (\d+\.\d+\.\d+)") { return $true, $matches[1] } else { return $false, $null } } catch { return $false, $null } } # Function to get installation statistics function Get-InstallationStats { $installDate = Get-Date $osVersion = (Get-ComputerInfo).WindowsVersion return @{ InstallDate = $installDate OSVersion = $osVersion } } # Function to uninstall Python function Uninstall-Python { Write-Output "Uninstalling Python..." $pythonPath = (Get-Command python).Path $uninstallerPath = Join-Path (Split-Path $pythonPath -Parent) "uninstall.exe" if (Test-Path $uninstallerPath) { Start-Process -FilePath $uninstallerPath -ArgumentList "/quiet" -Wait Write-Output "Python uninstalled successfully." } else { Write-Output "Uninstaller not found. Manual uninstallation may be required." } } # Check if the -Uninstall flag is provided if ($Uninstall) { $pythonInstalled, $installedVersion = Check-PythonInstalled if ($pythonInstalled) { Uninstall-Python } else { Write-Output "Python is not installed." } exit } # Check if Python is already installed $pythonInstalled, $installedVersion = Check-PythonInstalled if ($pythonInstalled) { Write-Output "Python is already installed." Write-Output "Version: $installedVersion" Write-Output "Installation path: $(Get-Command python).Path" } else { Write-Output "Python is not installed. Proceeding with installation..." # Define the URL for the latest Python 3 installer $pythonUrl = "https://www.python.org/ftp/python/3.13.0/python-3.13.0-amd64.exe" $pythonInstaller = "$env:TEMP\python-installer.exe" # Output message: Starting download Write-Output "Downloading Python installer from $pythonUrl..." # Download the Python installer # @DESC: Downloads the Python installer from the specified URL and saves it to the defined path Invoke-WebRequest -Uri $pythonUrl -OutFile $pythonInstaller # Output message: Download complete Write-Output "Download complete. Installing Python..." # Install Python silently and add it to the PATH # @DESC: Executes the installer with silent mode, installs for all users, and adds Python to the system PATH Start-Process -FilePath $pythonInstaller -ArgumentList "/quiet InstallAllUsers=1 PrependPath=1" -Wait # Output message: Installation complete Write-Output "Installation complete. Cleaning up..." # Clean up the installer file # @DESC: Removes the installer file after installation to free up space Remove-Item -Path $pythonInstaller -Force # Output message: Cleanup complete Write-Output "Cleanup complete. Verifying installation..." # Verify the installation # @DESC: Prints the installed Python version to verify successful installation $pythonVersion = python --version Write-Output "Python installation and verification complete." Write-Output "Installed version: $pythonVersion" # Get installation statistics $stats = Get-InstallationStats Write-Output "Installation Date: $($stats.InstallDate)" Write-Output "Operating System Version: $($stats.OSVersion)" }