Skip to content

Instantly share code, notes, and snippets.

@gabriel-vanca
Last active July 19, 2024 03:07
Show Gist options
  • Save gabriel-vanca/38326aff0db80fd96d14646af3531d4e to your computer and use it in GitHub Desktop.
Save gabriel-vanca/38326aff0db80fd96d14646af3531d4e to your computer and use it in GitHub Desktop.
Refreshing PowerShell Terminal
# Test Command Existence
function Test-CommandExists {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] $command
)
$exists = $null -ne (Get-Command $command -ErrorAction SilentlyContinue)
return $exists
}
function reload {
Write-Verbose "Refreshing PowerShell profile"
& $PROFILE
# ($NULL -eq $IsWindows) checks for Windows Sandbox enviroment
if ($IsWindows -or ($NULL -eq $IsWindows)) {
Write-Verbose "Refreshing the current Windows session environment variables."
# Expected path of the choco.exe file.
$chocoInstallPath = "$Env:ProgramData/chocolatey/choco.exe"
if ((Test-Path -path $chocoInstallPath) -and (Test-CommandExists choco) -and (Test-CommandExists Update-SessionEnvironment)) {
# Make `refreshenv` available right away, by defining the $env:ChocolateyInstall
# variable and importing the Chocolatey profile module.
$env:ChocolateyInstall = Convert-Path "$((Get-Command choco).Path)\..\.."
Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
Update-SessionEnvironment # refreshenv is an alias
}
else {
Write-Warning "Update-SessionEnvironment was not available. Enviroment variables not refreshed. You might have to restart the terminal session."
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment