能否通过PowerShell脚本直接运行GitHub仓库中的安装可执行文件(无需下载)
Great question! Let’s break this down clearly: the short answer is no, you can’t directly run a Windows .exe file from a GitHub repo without first saving it to your local system. Here’s why, plus a clean workaround to achieve what you’re aiming for.
Why Direct Execution Isn’t Feasible
Windows relies on executable files being present on a local filesystem (physical or virtual) to load and execute them. GitHub serves raw file content over HTTP, which the OS doesn’t recognize as a valid executable path. Unlike script files (like .ps1 or .bat) that can be piped into an interpreter, binary .exe files need to exist as local files for Windows to handle their execution properly.
A Clean Workaround: Temp Folder Download & Auto-Cleanup
The best approach is to download the installer to your system’s temporary directory, run it, then delete the file right after. This keeps your system tidy while getting the job done. Here’s a PowerShell script tailored for the Smartmontools repo:
# Replace [VERSION] with the actual release version (e.g., 7.4) from the repo's Releases page $installerUrl = "https://github.com/rarehalf/Smartmontools/releases/download/[VERSION]/smartmontools-[VERSION]-setup.exe" $tempFilePath = Join-Path $env:TEMP "smartmontools-installer.exe" try { # Download the installer to temp folder Write-Host "Downloading Smartmontools installer..." Invoke-WebRequest -Uri $installerUrl -OutFile $tempFilePath -UseBasicParsing # Run the installer (add arguments like /S for silent install if supported) Write-Host "Starting installation..." Start-Process -FilePath $tempFilePath -ArgumentList "/S" -Wait -NoNewWindow Write-Host "Installation finished successfully!" } catch { Write-Error "Oops, something went wrong: $_" } finally { # Clean up the temp file immediately if (Test-Path $tempFilePath) { Write-Host "Cleaning up temporary installer file..." Remove-Item $tempFilePath -Force } }
Quick Notes:
- Head to the Smartmontools repo’s Releases page to grab the exact URL for the installer version you want (replace
[VERSION]in the script). - Adjust the
-ArgumentListvalue if you need silent installation or other setup options (check the installer’s documentation for supported flags). - We use
$env:TEMPso the file is stored in a system-managed temporary location, and explicitly delete it afterward for immediate cleanup.
Alternative: Use a Package Manager
If you’d rather skip writing a script, check if Smartmontools is available via a package manager like Winget or Chocolatey. For example, with Winget:
winget install rarehalf.Smartmontools
This handles downloading, installing, and cleaning up automatically—no manual steps required.
内容的提问来源于stack exchange,提问作者Evtim Kostadinov

