Skip to content

Instantly share code, notes, and snippets.

@Daethyra
Created August 19, 2023 00:51
Show Gist options
  • Save Daethyra/66c78005b8e9c8e1dc28dd9e43021596 to your computer and use it in GitHub Desktop.
Save Daethyra/66c78005b8e9c8e1dc28dd9e43021596 to your computer and use it in GitHub Desktop.
Python 3.10.11 PowerShell Auto-Installation
# PowerShell Script to Download and Install Python 3.10.11
# This script automates the process of downloading and installing Python 3.10.11.
# It includes error handling, version checking, admin privilege checking, installer validation, and post-installation cleanup.
# Check if Python 3.10.11 is already installed
$installedVersion = & python --version 2>&1
if ($installedVersion -match "Python 3.10.11") {
Write-Host "Python 3.10.11 is already installed. Exiting..."
exit
}
# Parse Command-Line Arguments
$installForAllUsers = $false
if ($args -contains '--all-users') {
$installForAllUsers = $true
}
# Check for Admin Privileges if Necessary
if ($installForAllUsers) {
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if (!$isAdmin) {
Write-Host "Installing for all users requires administrative privileges. Please run the script as an administrator or remove the '--all-users' argument."
exit
}
}
# Setup variables
$pythonUrl = "https://www.python.org/ftp/python/3.10.11/python-3.10.11-amd64.exe"
$pythonHash = "a55e9c1e6421c84a4bd8b4be41492f51"
$pythonInstaller = "$($env:TEMP)\python.exe"
# Download Python installer
Write-Host "Downloading Python installer..."
try {
Invoke-WebRequest -Uri $pythonUrl -OutFile $pythonInstaller
$installerHash = Get-FileHash $pythonInstaller -Algorithm MD5 | ForEach-Object Hash
if ($installerHash -ne $pythonHash) {
Write-Host "Hash mismatch! Installer may have been tampered with. Exiting..."
exit
}
} catch {
Write-Host "Failed to download Python installer. Please check your internet connection and try again."
exit 1
}
# Install Python
Write-Host "Installing Python..."
try {
$installAllUsersArg = if ($installForAllUsers) { "1" } else { "0" }
Start-Process -FilePath $pythonInstaller -ArgumentList "/quiet InstallAllUsers=$installAllUsersArg PrependPath=1" -Wait
} catch {
Write-Host "Python installation failed:" $_
exit 1
}
# Post-installation cleanup
Write-Host "Cleaning up downloaded files..."
Remove-Item -Path $pythonInstaller -Force
Write-Host "Python installation complete!"
@Daethyra
Copy link
Author

Daethyra commented Jul 13, 2024

can i request to set a argument for download another py version? please

You can find the correct Python URL on their website. If there was a scope for this script, adding CLI args wouldn't be in it.


I might've misunderstood what you meant. You can directly change which version of Python the program checks for by editing the $installedVersion, $pythonUrl, and $pythonHash variables. Make sure you confirm the MD5 hash manually, yourself, before editing it into the script. You should be able to find the hash on whatever page you download Python from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment