Skip to content

Instantly share code, notes, and snippets.

@raoshaab
Last active July 7, 2024 20:58
Show Gist options
  • Save raoshaab/de861c353ab0e0cb77f8afd5325e43c1 to your computer and use it in GitHub Desktop.
Save raoshaab/de861c353ab0e0cb77f8afd5325e43c1 to your computer and use it in GitHub Desktop.
Install Python 3 with powershell in windows 10
# Download the latest version of Python from the official website
$pythonUrl = "https://www.python.org/ftp/python/3.10.0/python-3.10.0-amd64.exe"
$pythonInstaller = "$($env:TEMP)\python.exe"
Invoke-WebRequest -Uri $pythonUrl -OutFile $pythonInstaller
# Install Python with default settings
Start-Process -FilePath $pythonInstaller -ArgumentList "/quiet" -Wait
# Add Python to the PATH environment variable
$pythonPath = Join-Path $env:ProgramFiles "Python310"
[System.Environment]::SetEnvironmentVariable("Path", "$($env:Path);$pythonPath", "User")
# Verify the installation
python --version
@Daethyra
Copy link

Thanks friend. ♥

Small problem 😖

Seems to have worked fine, minus:

PS C:\Users\{USER}\Downloads> .\py-install.ps1
Exception calling "SetEnvironmentVariable" with "3" argument(s): "Requested registry access is not allowed."
At C:\Users\{USER}\Downloads\py-install.ps1:11 char:57
+ ... tem.Environment]::SetEnvironmentVariable("Path", "$($env:Path);$pytho ...
+~~~~~~~~~
  + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException                                               
  + FullyQualifiedErrorId : SecurityException

My Fix

  • "Machine" -> "User"
# Download the latest version of Python from the official website
$pythonUrl = "https://www.python.org/ftp/python/3.10.0/python-3.10.12-amd64.exe"
$pythonInstaller = "$($env:TEMP)\python.exe"
Invoke-WebRequest -Uri $pythonUrl -OutFile $pythonInstaller

# Install Python with default settings
Start-Process -FilePath $pythonInstaller -ArgumentList "/quiet" -Wait

# Add Python to the PATH environment variable
$pythonPath = Join-Path $env:ProgramFiles "Python310"
[System.Environment]::SetEnvironmentVariable("Path", "$($env:Path);$pythonPath", "User")

# Verify the installation
python --version

@Daethyra
Copy link

I created my own version based off of your work. Thanks again.

@raoshaab
Copy link
Author

Now Updated !!

@raoshaab
Copy link
Author

raoshaab commented Jul 7, 2024

You can define the install directory and supply it as new argument TargetDir,


# Define the target installation directory i.e $installDir = "D:\newfolder\python3"
$installDir = "D:\Python3"

# Install Python with specified installation directory
Start-Process -FilePath $pythonInstaller -ArgumentList "/quiet  PrependPath=1 TargetDir=$installDir" -Wait

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