Last active
July 14, 2024 01:10
-
-
Save codebytes/1ae354e736c88adef5b6f802597e3101 to your computer and use it in GitHub Desktop.
Powershell script to install nuget and update path
This file contains 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
$sourceNugetExe = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" | |
$targetPath = "C:\Program Files\nuget" | |
if (-not (Test-Path -LiteralPath $targetPath)) { | |
try { | |
New-Item -Path $targetPath -ItemType Directory -ErrorAction Stop | Out-Null #-Force | |
} | |
catch { | |
Write-Error -Message "Unable to create directory '$targetPath'. Error was: $_" -ErrorAction Stop | |
} | |
"Successfully created directory '$targetPath'." | |
} | |
else { | |
"$targetPath already existed" | |
} | |
if (-not (Test-Path -LiteralPath "$targetPath\nuget.exe")) { | |
"Downloading nuget.exe to $targetPath\nuget.exe" | |
Invoke-WebRequest $sourceNugetExe -OutFile "$targetPath\nuget.exe" | |
} else { | |
"nuget.exe already exists at $targetPath" | |
} | |
# Add the tools dir to the path which directly contains NuGet.exe and CredentialProvider.VSS.exe | |
if (!($env:Path -like "*;$targetPath*")) | |
{ | |
"Adding $targetPath to Path" | |
$env:Path = "$targetPath;" + $env:Path | |
$oldPath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name path).path | |
$newPath=$oldPath+";$targetPath" | |
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name path -Value $newPath | |
} | |
else { | |
"$targetPath already existed in Path" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run from an elevated powershell with:
iex ((New-Object System.Net.WebClient).DownloadString('https://gist.github.com/Codebytes/1ae354e736c88adef5b6f802597e3101/raw'))