Hello Varma
I modified it a little and tested it both locally and on an Image in my own gallery. I added some comment and explanation in the code, feel free to remove them or modify them to your liking :)
Don't hesitate to ask if you run into trouble with it.
#Creates the directory Java in C:
New-Item -ItemType Directory -Path "c:\Java"
#Downloads the Java MSI file, I changed from Invoke-Webrequest to Invoke-RestMethod to ensure it waits for the download to complete before moving on
Invoke-RestMethod -uri "https://download.oracle.com/java/21/latest/jdk-21_windows-x64_bin.msi" -OutFile "c:\Java\jdk-21_windows-x64_bin.msi"
#Starts the installation, also added a Wait command here to ensure the installation is complete before moving on to next step, added /quiet to ensure no user interaction is needed.
Start-Process msiexec.exe -Wait -ArgumentList "/I c:\Java\jdk-21_windows-x64_bin.msi /quiet"
#Shortened the environmental command by using the command below, it creates a Environmental variable called #JAVA_HOME with the value of 'C:\Program Files\Java\jdk-21\bin'
#$Env:JAVA_HOME = 'C:\Program Files\Java\jdk-21\bin'
#Added back to set the variable to system instead of user
[System.Environment]::SetEnvironmentVariable("JAVA_HOME","C:\Program Files\Java\jdk-21\bin","Machine")
#Writes out the content of the environmental variable JAVA_HOME
Write-Host $Env:JAVA_HOME "has been added to the environmental variable JAVA_HOME"
#Creates the directory Python in C:
New-Item -ItemType Directory -Path "c:\Python"
#Same download procedure as with java but for Python
Invoke-RestMethod -uri "https://www.python.org/ftp/python/3.12.1/python-3.12.1-amd64.exe" -OutFile "c:\Python\python-3.12.1-amd64.exe"
#Same installation procedure as with java but EXE file instead and added /silent to ensure no user interaction is needed. Added the InstallAllUser=1 to ensure its installed for all users.
Start-Process -Wait -FilePath "c:\Python\python-3.12.1-amd64.exe" "/quiet InstallAllUsers=1 PrependPath=1"
Hope this is helpful Best Regards, Timmy Malmgren
---If the Answer is helpful, please click "Accept Answer" and upvote it as it helps others to find what they are looking for faster!