Last active
August 3, 2024 09:06
-
-
Save asheroto/5c1711fcbd888c5a621b1ff0b91dfc00 to your computer and use it in GitHub Desktop.
Functions to instantly get, set, or delete an environment variable. Bypasses the typical delay experienced with Environment.SetEnvironmentVariable, which can be slow due to its broadcasting a message to all top-level windows. These functions offer a more efficient alternative for getting, setting, or deleting environment variables.
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
# See this for more info: | |
# https://stackoverflow.com/questions/4825967/environment-setenvironmentvariable-takes-a-long-time-to-set-a-variable-at-user-o | |
function Set-EnvironmentVariable { | |
<# | |
.SYNOPSIS | |
Instantly sets an environment variable in the machine or user environment by updating the registry. | |
.DESCRIPTION | |
Instantly sets an environment variable in the machine or user environment by updating the registry. | |
.PARAMETER Name | |
The name of the environment variable to set. | |
.PARAMETER Value | |
The value of the environment variable to set. | |
.PARAMETER Target | |
The target environment to set the environment variable in. Valid values are 'Machine' and 'User'. | |
.EXAMPLE | |
Set-EnvironmentVariable -Name "MyVariable" -Value "MyValue" -Target "Machine" | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[ValidateNotNullOrEmpty()] | |
[string]$Name, | |
[Parameter(Mandatory = $true)] | |
[ValidateNotNullOrEmpty()] | |
[string]$Value, | |
[Parameter(Mandatory = $true)] | |
[ValidateSet("Machine", "User")] | |
[string]$Target | |
) | |
$regKey = switch ($Target.ToLower()) { | |
"machine" { 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' } | |
"user" { 'HKCU:\Environment' } | |
} | |
Set-ItemProperty -Path $regKey -Name $Name -Value $Value | |
} |
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 Get-EnvironmentVariable { | |
<# | |
.SYNOPSIS | |
Retrieves the value of an environment variable from the machine or user environment by accessing the registry. | |
.DESCRIPTION | |
Retrieves the value of an environment variable from the machine or user environment by accessing the registry. | |
.PARAMETER Name | |
The name of the environment variable to retrieve. | |
.PARAMETER Target | |
The target environment to retrieve the environment variable from. Valid values are 'Machine' and 'User'. | |
.EXAMPLE | |
Get-EnvironmentVariable -Name "MyVariable" -Target "Machine" | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[ValidateNotNullOrEmpty()] | |
[string]$Name, | |
[Parameter(Mandatory = $true)] | |
[ValidateSet("Machine", "User")] | |
[string]$Target | |
) | |
$regKey = switch ($Target.ToLower()) { | |
"machine" { 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' } | |
"user" { 'HKCU:\Environment' } | |
} | |
$existingProperties = Get-Item -Path $regKey | Select-Object -ExpandProperty Property | |
if ($existingProperties -contains $Name) { | |
$value = Get-ItemProperty -Path $regKey -Name $Name | Select-Object -ExpandProperty $Name | |
return $value | |
} else { | |
Write-Warning "Environment variable '$Name' does not exist in the $Target environment." | |
return $null | |
} | |
} |
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 Remove-EnvironmentVariable { | |
<# | |
.SYNOPSIS | |
Instantly removes an environment variable from the machine or user environment by updating the registry. | |
.DESCRIPTION | |
Instantly removes an environment variable from the machine or user environment by updating the registry. | |
.PARAMETER Name | |
The name of the environment variable to remove. | |
.PARAMETER Target | |
The target environment to remove the environment variable from. Valid values are 'Machine' and 'User'. | |
.EXAMPLE | |
Remove-EnvironmentVariable -Name "MyVariable" -Target "Machine" | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[ValidateNotNullOrEmpty()] | |
[ValidateScript({ | |
if ($_.ToLower() -eq "path") { | |
throw "Cannot remove the 'Path' environment variable for safety reasons." | |
} | |
return $true | |
})] | |
[string]$Name, | |
[Parameter(Mandatory = $true)] | |
[ValidateSet("Machine", "User")] | |
[string]$Target | |
) | |
$regKey = switch ($Target.ToLower()) { | |
"machine" { 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' } | |
"user" { 'HKCU:\Environment' } | |
} | |
$existingProperties = Get-Item -Path $regKey | Select-Object -ExpandProperty Property | |
if ($existingProperties -contains $Name) { | |
# Write-Output "Removing environment variable '$Name' from the $Target environment." | |
Remove-ItemProperty -Path $regKey -Name $Name | |
} else { | |
Write-Warning "Environment variable '$Name' does not exist in the $Target environment." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment