Created
September 13, 2023 00:16
-
-
Save Silvenga/e7003c10987b3c3352c63b899962d08b to your computer and use it in GitHub Desktop.
A simple PS script to install ADB/Fastboot (aka platform-tools) directly from Google and add the the binaries to PATH.
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
function Update-UserPath([string]$Value) | |
{ | |
$currentPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::User) | |
if ($currentPath -like "*${Value}*") | |
{ | |
Write-Host "The value '${Value}' already exists in user PATH, no update is needed." | |
} | |
else | |
{ | |
$targetPath = $currentPath + ";${Value}"; | |
[Environment]::SetEnvironmentVariable("Path", $currentPath + ";${Value}", [EnvironmentVariableTarget]::User) | |
Write-Host "Updated user PATH from '${currentPath}' to '${targetPath}'." | |
Write-Host "Make sure to restart your applications to get the updated PATH." | |
} | |
} | |
## Main | |
if ($Env:OS -ne "Windows_NT") | |
{ | |
throw "${Env:OS} is not supported by this script at this time."; | |
} | |
$downloadUri = 'https://dl.google.com/android/repository/platform-tools-latest-windows.zip' | |
$workspacePath = Join-Path $env:LOCALAPPDATA 'adb-tools' | |
$tempPath = Join-Path $workspacePath 'tmp' | |
$binPath = Join-Path $workspacePath 'bin' | |
mkdir $workspacePath -ErrorAction Ignore | Out-Null | |
mkdir $tempPath -ErrorAction Ignore | Out-Null | |
mkdir $binPath -ErrorAction Ignore | Out-Null | |
Write-Host "[1/4] Download latest platform-tools from '${downloadUri}'..." | |
$archivePath = Join-Path $tempPath 'platform-tools-latest-windows.zip' | |
$ProgressPreference = 'SilentlyContinue' | |
try | |
{ | |
Invoke-WebRequest -Uri $downloadUri -OutFile $archivePath | |
} | |
finally | |
{ | |
# Bad implementation of a progress bar... | |
$ProgressPreference = 'Continue' | |
} | |
Write-Host "Done." | |
Write-Host "[2/4] Extracting downloaded platform-tools..." | |
$extractPath = Join-Path $tempPath 'platform-tools-latest-windows' | |
if (Test-Path $extractPath) | |
{ | |
Write-Host "Extract path '${extractPath}' already exists, removing..." | |
Remove-Item $extractPath -Recurse | |
} | |
Expand-Archive -Path $archivePath -DestinationPath $extractPath | |
Write-Host "Done." | |
Write-Host "[3/4] Installing platform tools..." | |
$platformToolsPath = Join-Path $extractPath 'platform-tools' | |
if (Test-Path $binPath) | |
{ | |
Write-Host "Install path '${binPath}' already exists, removing..." | |
Remove-Item $binPath -Recurse | |
} | |
mkdir $binPath | Out-Null | |
Copy-Item -Recurse -Path "${platformToolsPath}\*" -Destination $binPath | |
Write-Host "Done." | |
Write-Host "[4/4] Updating user PATH..." | |
Update-UserPath -Value $binPath | |
Write-Host "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment