# Script to install Python on Windows devices

Python is a powerful and versatile programming language with applications ranging from web development and data analysis to scientific computing and beyond.

In IT environments where Python is widely used, it’s crucial to ensure that all developers have the required version of Python installed for their specific projects. This not only prevents compatibility issues during collaborative projects but also guarantees that developers have access to the necessary features, bug fixes, security updates, and associated libraries or packages specific to that version. For example, a team or department may be working on a project that requires a specific Python version and associated libraries or packages. In such cases, using a different version of Python could lead to compatibility issues, potentially delaying the project and introducing errors. To ensure consistency across developer environments, IT administrators can utilize Hexnode’s [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) action to seamlessly install the required version of Python on Windows devices by executing the provided scripts.

 Disclaimer: 
The sample script provided below are adapted from third-party open source sites.

 

PowerShell script to install Python 
------------------------------------

PowerShell script to install Python on Windows devices

\# Define the Python version and download URL $pythonVersion = "<version>" $pythonDownloadUrl = "https://www.python.org/ftp/python/$pythonVersion/python-$pythonVersion-amd64.exe" # Define the installation directory $installDir = "C:\\Python" # Download Python installer Invoke-WebRequest -Uri $pythonDownloadUrl -OutFile "$env:TEMP\\python-installer.exe" # Install Python silently Start-Process -FilePath "$env:TEMP\\python-installer.exe" -ArgumentList "/quiet", "InstallAllUsers=1", "PrependPath=1", "DefaultCustomInstall=1", "DefaultPath=$installDir" -Wait # Clean up Remove-Item "$env:TEMP\\python-installer.exe" -Force

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

  \# Define the Python version and download URL 

$pythonVersion = "<version>"

$pythonDownloadUrl = "https://www.python.org/ftp/python/$pythonVersion/python-$pythonVersion-amd64.exe"

\# Define the installation directory 

$installDir = "C:\\Python"

\# Download Python installer 

Invoke-WebRequest -Uri $pythonDownloadUrl -OutFile "$env:TEMP\\python-installer.exe"

\# Install Python silently 

Start-Process -FilePath "$env:TEMP\\python-installer.exe" -ArgumentList "/quiet", "InstallAllUsers=1", "PrependPath=1", "DefaultCustomInstall=1", "DefaultPath=$installDir" -Wait

\# Clean up 

Remove-Item "$env:TEMP\\python-installer.exe" -Force

   

 

  

This PowerShell script begins by defining variables for the desired Python version and its download URL. The placeholder **“version”** can be replaced with the specific version number needed. Subsequently, the script defines the installation directory path. Utilizing the **Invoke-WebRequest** cmdlet, the script downloads the Python installer from the specified URL and saves it in a temporary directory. To ensure a silent installation without any user interface or interaction, the script employs the **Start-Process** cmdlet with the **/quiet** argument. The script waits for the installation to finish using the **-Wait** parameter. It then cleans up by removing the downloaded Python installer from the temporary directory, bypassing confirmation prompts with the **-Force** parameter.

Batch script to install Python 
-------------------------------

Batch script to install Python on Windows devices

@echo off rem Define the Python version and download URL set "pythonVersion=<version>" set "pythonDownloadUrl=https://www.python.org/ftp/python/%pythonVersion%/python-%pythonVersion%-amd64.exe" rem Define the installation directory set "installDir=C:\\Python" rem Download Python installer bitsadmin.exe /transfer "PythonInstaller" "%pythonDownloadUrl%" "%TEMP%\\python-installer.exe" rem Install Python silently "%TEMP%\\python-installer.exe" /quiet InstallAllUsers=1 PrependPath=1 DefaultCustomInstall=1 DefaultPath=%installDir% /wait rem Clean up del "%TEMP%\\python-installer.exe" /f /q 

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

  @echo off 

rem Define the Python version and download URL 

set "pythonVersion=<version>"

set "pythonDownloadUrl=https://www.python.org/ftp/python/%pythonVersion%/python-%pythonVersion%-amd64.exe"

rem Define the installation directory 

set "installDir=C:\\Python"

rem Download Python installer 

bitsadmin.exe /transfer "PythonInstaller" "%pythonDownloadUrl%" "%TEMP%\\python-installer.exe"

rem Install Python silently

"%TEMP%\\python-installer.exe" /quiet InstallAllUsers=1 PrependPath=1 DefaultCustomInstall=1 DefaultPath=%installDir% /wait 

rem Clean up 

del "%TEMP%\\python-installer.exe" /f /q 

   

 

  

This batch script starts by defining variables for the desired Python version and its download URL. The placeholder **“version”** can be substituted with the specific version number needed. Next, the installation directory is defined. The script then employs the **bitsadmin.exe** command-line tool to download the Python installer from the specified URL to a temporary location. After the download is complete, the script executes the Python installer to install Python silently using the **/quiet** argument. Finally, the script deletes the Python installer executable from the temporary location to clean up.

What happens at the device end?
-------------------------------

After executing the script, Python will be silently installed on the Windows device. At the device end, IT admins can use the *“python”* command in the PowerShell terminal to verify the installation of Python. This command checks if Python is installed and displays the installed Python version.

[![The script to install Python on Windows devices can be validated by running the python command from the device](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/05/Script-to-install-Python-can-be-verified-using-the-python-command-on-the-device-end.png "Script to install Python can be verified using the python command on the device end")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/05/Script-to-install-Python-can-be-verified-using-the-python-command-on-the-device-end.png)

Alternatively, IT admins can check the installed Python version by executing the following scripts:

### PowerShell: 

PowerShell script to check the version of Python installed

\# Check if python is installed $pythonInstalled = Get-Command python -ErrorAction SilentlyContinue if ($pythonInstalled) { # Get the Python version $pythonVersion = python --version 2>&1 # Print the Python version Write-Host "Python version: $pythonVersion" } else { Write-Host "Python is not installed on this device." } 

   1

2

3

4

5

6

7

8

9

10

11

12

  \# Check if python is installed 

$pythonInstalled = Get-Command python -ErrorAction SilentlyContinue 

if ($pythonInstalled) {

 \# Get the Python version 

 $pythonVersion = python --version 2>&1

 \# Print the Python version 

 Write-Host "Python version: $pythonVersion"

} else {

 Write-Host "Python is not installed on this device."

} 

   

 

  

This PowerShell script checks for the presence of Python on a system by attempting to find the **python** command in the system’s PATH. If Python is installed, it retrieves its version using **python –version**. The script then prints the Python version to the console. If Python is not found, it displays a message indicating its absence.

[![After successfully executing the script to install Python on Windows, it shows the installed version as output in Hexnode](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/05/Script-to-install-Python-on-Windows-shows-the-version-as-output.png "Script to install Python on Windows shows the version as output")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/05/Script-to-install-Python-on-Windows-shows-the-version-as-output.png)

### Batch:

Batch script to check the version of Python installed

@echo off setlocal set "python\_exe=python" REM Check if Python executable exists where %python\_exe% >nul 2>nul if %errorlevel% neq 0 ( echo Python is not installed on this device. goto :eof ) REM Get Python version for /f "tokens=\*" %%v in ('%python\_exe% -V 2^>^&1') do set "python\_version=%%v" echo Python Version: %python\_version% endlocal 

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

  @echo off 

setlocal 

set "python\_exe=python"

REM Check if Python executable exists 

where %python\_exe% >nul 2>nul 

if %errorlevel% neq 0 (

 echo Python is not installed on this device.

 goto :eof

)

REM Get Python version 

for /f "tokens=\*" %%v in ('%python\_exe% -V 2^>^&1') do set "python\_version=%%v"

echo Python Version: %python\_version%

endlocal 

   

 

  

This batch script first defines the variable **python\_exe** as *“python”*, representing the Python executable name. It then checks if Python is installed by attempting to locate its executable using **where** command. If the executable is not found (i.e., the error level is not equal to 0), the script outputs a message indicating that Python is not installed. If Python is found, it retrieves its version using the **%python\_exe% -V** command and displays it.

[![If Python is not installed on Windows, an error message will be displayed when running the script](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/05/If-Python-isnt-installed-on-Windows-the-script-displays-an-error.png "If Python isn't installed on Windows, the script displays an error")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/05/If-Python-isnt-installed-on-Windows-the-script-displays-an-error.png)

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.