Created
June 26, 2026 06:57
-
-
Save lbr88/c815168d6ade4f35eb989169535e1688 to your computer and use it in GitHub Desktop.
Install Github cli on machine with not admin access.
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 Install-GitHubCli { | |
| # Create .gh folder if it doesn't exist | |
| $userProfileFolder = [Environment]::GetFolderPath('UserProfile') | |
| $ghFolder = Join-Path -Path $userProfileFolder -ChildPath '.gh' | |
| if (-not (Test-Path -LiteralPath $ghFolder)) { | |
| New-Item -Path $ghFolder -ItemType Directory > $null | |
| } | |
| # Get the main releases page HTML | |
| $releasesUrl = 'https://github.com/cli/cli/releases' | |
| $releasesHtml = Invoke-WebRequest -Uri $releasesUrl -UseBasicParsing | |
| # Identify the latest version using the first release link | |
| if ($releasesHtml.Content -notmatch '/cli/cli/releases/tag/(?<Version>[^"]+)') { | |
| Write-Error -Message 'No release link found.' | |
| return | |
| } | |
| $version = $matches['Version'] | |
| # Download the zip file | |
| $downloadUrl = "https://github.com/cli/cli/releases/download/${version}/gh_$($version -replace '^v')_windows_amd64.zip" | |
| $zipFileName = "gh_${version}_windows_amd64.zip" | |
| $zipPath = Join-Path -Path $env:TEMP -ChildPath $zipFileName | |
| Invoke-WebRequest -Uri $downloadUrl -OutFile $zipPath | |
| # Extract gh.exe using Expand-Archive | |
| Expand-Archive -LiteralPath $zipPath -DestinationPath $ghFolder -Force | |
| # Clean up | |
| Remove-Item -LiteralPath $zipPath | |
| # Add to environment PATH for the current process and future processes via a user environment setting update if the path is not already present | |
| $currentPath = [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::Process) | |
| $ghExeFolder = Join-Path -Path $ghFolder -ChildPath 'bin' | |
| if ($currentPath.Split(';') -notcontains $ghExeFolder) { | |
| ${env:Path} = "${currentPath};${ghExeFolder}" | |
| [Environment]::SetEnvironmentVariable('Path', ${env:Path}, [EnvironmentVariableTarget]::Process) | |
| [Environment]::SetEnvironmentVariable('Path', ${env:Path}, [EnvironmentVariableTarget]::User) | |
| } | |
| Write-Output "gh.exe installed to ${ghFolder} and ${ghExeFolder} added to PATH." | |
| } | |
| Install-GitHubCli |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment