@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:: Variables
SET DOWNLOAD_URL=https://www.python.org/ftp/python/3.12.0/python-3.12.0-amd64.exe
SET PYTHON_INSTALLER=python-3.12.0-amd64.exe
SET PYTHON_PATH=%LOCALAPPDATA%ProgramsPythonPython312
SET SCRIPTS_PATH=%PYTHON_PATH%Scripts
:: Step 1: Download Python installer if it doesn't exist
IF EXIST "%~dp0%PYTHON_INSTALLER%" (
echo Python installer already exists.
) ELSE (
echo Downloading Python installer from %DOWNLOAD_URL%...
powershell -Command "Invoke-WebRequest -Uri '%DOWNLOAD_URL%' -OutFile '%~dp0%PYTHON_INSTALLER%'"
IF NOT EXIST "%~dp0%PYTHON_INSTALLER%" (
echo Failed to download the Python installer.
pause
exit /b 1
)
)
:: Step 2: Install Python silently
echo Installing Python...
"%~dp0%PYTHON_INSTALLER%" /quiet InstallAllUsers=0 PrependPath=0 TargetDir=%PYTHON_PATH%
:: Step 3: Verify Python was installed successfully
IF NOT EXIST "%PYTHON_PATH%python.exe" (
echo Python installation failed.
pause
exit /b 1
)
:: Step 4: Get the current user PATH (before adding Python)
FOR /F "tokens=2* delims= " %%A IN ('reg query "HKEY_CURRENT_USEREnvironment" /v Path 2^>nul') DO (
IF "%%A"=="Path" SET OLD_PATH=%%B
)
:: Step 5: Set new paths (Python and Scripts) to the new path variable
SET NEW_PATH=!OLD_PATH!;%PYTHON_PATH%;%SCRIPTS_PATH%
echo NEW_PATH (before removing duplicates): !NEW_PATH!
:: Step 6: Remove duplicates (optional)
SET NEW_PATH=!NEW_PATH:"%PYTHON_PATH%"=!
SET NEW_PATH=!NEW_PATH:"%SCRIPTS_PATH%"=!
:: Log final NEW_PATH after removing duplicates
echo NEW_PATH (after removing duplicates): !NEW_PATH!
:: Step 7: Update the system PATH for the user
SETX PATH "!NEW_PATH!"
:: Step 8: Update the current CMD session PATH (immediate effect)
SET PATH=!NEW_PATH!
:: Step 9: Verify Python installation
echo Verifying Python installation...
python --version
IF %ERRORLEVEL% NEQ 0 (
echo Python not found in PATH. You may need to restart your system.
pause
exit /b 1
)
echo Python 3.12.0 installed successfully, added to user PATH.
pause
exit /b 0
Prepared Batch file and running the batch file to download , Install python 3.12.0 and add the Environment variables to the Path.
But it deleting all the existing other app paths from environment path variables.
Note : I need to download and install from batch file only (Not Powershell )
10