Created
October 22, 2025 01:52
-
-
Save engalar/7e768ab678d62271141dac32cff1ec96 to your computer and use it in GitHub Desktop.
auto install powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ----------------------------------------------------------- | |
| # PowerShell 下载与安装脚本 | |
| # ----------------------------------------------------------- | |
| # 定义下载链接和目标文件名 | |
| $DownloadUrl = "https://github.com/PowerShell/PowerShell/releases/download/v7.5.4/PowerShell-7.5.4-win-x64.msi" | |
| $FileName = "PowerShell-7.5.4-win-x64.msi" | |
| $DownloadPath = Join-Path $env:TEMP $FileName | |
| Write-Host "--- 1. 正在检查 PowerShell 版本 ---" -ForegroundColor Cyan | |
| # 检查是否已安装该版本 (可选步骤,提供反馈) | |
| $InstalledVersion = (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.DisplayName -like "PowerShell 7-x64" }).DisplayVersion | |
| if ($InstalledVersion -eq "7.5.4") { | |
| Write-Host "PowerShell 7.5.4 已安装。脚本停止。" -ForegroundColor Green | |
| exit | |
| } | |
| Write-Host "--- 2. 正在下载文件: $FileName ---" -ForegroundColor Cyan | |
| Write-Host "URL: $DownloadUrl" | |
| try { | |
| # 使用 Invoke-WebRequest 进行下载 | |
| # -OutFile 指定保存路径 | |
| # -UseBasicParsing 避免在旧版PowerShell中出现警告 | |
| Invoke-WebRequest -Uri $DownloadUrl -OutFile $DownloadPath -UseBasicParsing | |
| Write-Host "下载成功!文件保存到: $DownloadPath" -ForegroundColor Green | |
| } catch { | |
| Write-Host "下载失败!请检查网络连接或URL是否正确。" -ForegroundColor Red | |
| Write-Host $_.Exception.Message -ForegroundColor Red | |
| exit 1 | |
| } | |
| Write-Host "--- 3. 正在安装 PowerShell ---" -ForegroundColor Cyan | |
| Write-Host "请稍候,安装程序可能需要管理员权限 UAC 提示。" | |
| # 执行 MSI 安装程序 | |
| # /i: 安装 | |
| # /qb: 静默安装,只显示进度条和完成信息 (如果需要完全静默,请使用 /qn) | |
| $InstallerArguments = @( | |
| "/i", | |
| "`"$DownloadPath`"", | |
| "/qb" | |
| ) | |
| try { | |
| # 启动 msiexec.exe 进行安装 | |
| $Process = Start-Process -FilePath msiexec.exe -ArgumentList $InstallerArguments -Wait -PassThru | |
| if ($Process.ExitCode -eq 0) { | |
| Write-Host "PowerShell 7.5.4 安装完成!" -ForegroundColor Green | |
| } else { | |
| Write-Host "安装失败。MSI 退出码: $($Process.ExitCode)" -ForegroundColor Red | |
| } | |
| } catch { | |
| Write-Host "安装程序启动失败。" -ForegroundColor Red | |
| Write-Host $_.Exception.Message -ForegroundColor Red | |
| exit 1 | |
| } | |
| Write-Host "--- 4. 清理下载文件 ---" -ForegroundColor Cyan | |
| try { | |
| Remove-Item $DownloadPath -Force | |
| Write-Host "临时文件已清理。" -ForegroundColor Yellow | |
| } catch { | |
| Write-Host "无法清理临时文件: $DownloadPath" -ForegroundColor Yellow | |
| } | |
| Write-Host "安装流程结束。您现在可以打开 'pwsh' 来运行新的 PowerShell 7。" -ForegroundColor Cyan | |
| # ----------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment