function EnsurePathIncludes($path) { $currentPath = [Environment]::GetEnvironmentVariable("Path", "Machine") $paths = $currentPath.Split(";") $found = $false foreach ($p in $paths) { if ($p -like $path) { $found = $true break } } if ($found -eq $false) { $newPath = $currentPath + ";$path" [Environment]::SetEnvironmentVariable("Path", $newPath, "Machine") Write-Host "Added $path to the PATH environment variable." } else { Write-Host "$path is already in the PATH environment variable." } } function DownloadPowershell($OUTPUT_FOLDER="C:\pwsh") { $START_URL = "https://github.com/PowerShell/PowerShell" $TEMP_DIR = "$OUTPUT_FOLDER\temp" if (Test-Path -Path $OUTPUT_FOLDER) { Write-Output "Removing existing $OUTPUT_FOLDER..." Remove-Item -Recurse -Force -Path $OUTPUT_FOLDER -ErrorAction Stop } Write-Output "Creating $OUTPUT_FOLDER..." $null = New-Item -ItemType Directory -Path $TEMP_DIR -Force Set-Location $TEMP_DIR $client = New-Object System.Net.WebClient Write-Output "Loading $START_URL..." $data = $client.DownloadString($START_URL) Write-Output "Finding latest version..." $regex = 'href="/PowerShell/PowerShell/releases/tag/(v.*)"' $mymatches = $data | Select-String -Pattern $regex -AllMatches $latestVersion = $mymatches.Matches.Groups[1].Value Write-Output "Latest version is $latestVersion" $latestVersionURL = "$START_URL/releases/tag/$latestVersion" Write-Output "Loading $latestVersionURL..." $data = $client.DownloadString($latestVersionURL) Write-Output "Finding $latestVersion zip..." $regex = '(PowerShell-.*-win-x64.zip)' $mymatches = $data | Select-String -Pattern $regex -AllMatches $latestZip = $mymatches.Matches.Groups[1].Value $latestZipURL = "$START_URL/releases/download/$latestVersion/$latestZip" Write-Output "Downloading $latestZipURL to $TEMP_DIR..." $null = $client.DownloadFile($latestZipURL, "$TEMP_DIR\$latestZip") Write-Output "Extracting $latestZip to $OUTPUT_FOLDER..." Add-Type -Assembly 'System.IO.Compression.FileSystem' [System.IO.Compression.ZipFile]::ExtractToDirectory("$TEMP_DIR\$latestZip", $OUTPUT_FOLDER) EnsurePathIncludes $OUTPUT_FOLDER Set-Location $HOME } DownloadPowershell