Skip to content

Instantly share code, notes, and snippets.

@codebytes
Last active July 5, 2021 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebytes/1ae354e736c88adef5b6f802597e3101 to your computer and use it in GitHub Desktop.
Save codebytes/1ae354e736c88adef5b6f802597e3101 to your computer and use it in GitHub Desktop.
Powershell script to install nuget and update path
$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"
}
@codebytes
Copy link
Author

Run from an elevated powershell with: iex ((New-Object System.Net.WebClient).DownloadString('https://gist.github.com/Codebytes/1ae354e736c88adef5b6f802597e3101/raw'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment