The following command successfully installs Python on Windows 11 when run from the PowerShell command line as Administrator:
c:/temp/python-3.11.4-amd64.exe /quiet InstallAllUsers=0 InstallLauncherAllUsers=0 PrependPath=1 Include_test=0
But when that same command is placed inside a script myinstallscript.ps1
, and when that script is called from the PowerShell command line as .\myinstallscript.ps1
, the installation fails without throwing any error.
Here is the relevant script code, including the same command that does not work when it is invoked in a script:
Write-Output "About to create temp folder. "
New-Item -ItemType Directory -Force -Path C:\temp
Write-Output "About to set security protocol. "
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Write-Output "About to download Python executable to temp folder. "
Invoke-WebRequest -Uri "https://www.python.org/ftp/python/3.11.4/python-3.11.4-amd64.exe" -OutFile "c:/temp/python-3.11.4-amd64.exe"
Write-Output "About to install Python. "
c:/temp/python-3.11.4-amd64.exe /quiet InstallAllUsers=0 InstallLauncherAllUsers=0 PrependPath=1 Include_test=0
Write-Output "About to append python to path temporarily. "
$env:Path += ";$($env:LOCALAPPDATA)\Programs\Python\Python311\;$($env:LOCALAPPDATA)\Programs\Python\Python311\Scripts\"
What specific syntax needs to be used in myinstallscript.ps1
in order to successfully execute the c:/temp/python-3.11.4-amd64.exe /quiet InstallAllUsers=0 InstallLauncherAllUsers=0 PrependPath=1 Include_test=0
command? And what specific syntax would be needed in order for the command to gracefully break the program with a useful error message in the event that the installation fails for some unforeseen reason?