Skip to content

Instantly share code, notes, and snippets.

@alphp
Created August 27, 2018 04:05
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 alphp/40ee173d4bbde03fc872489f7e07c946 to your computer and use it in GitHub Desktop.
Save alphp/40ee173d4bbde03fc872489f7e07c946 to your computer and use it in GitHub Desktop.
PowerShell module for Get/Set/Remove environment variables in Windows
function Get-Environment {
param (
[parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()]$Name,
[String] $DefaultValue = $null,
[ValidateSet("None", "DoNotExpandEnvironmentNames")] [String] $Options = "None"
)
([Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment", $true)).GetValue($Name, $DefaultValue, $Options)
}
function Set-Environment {
Param (
[parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String] $Name,
[String] $Value = $null,
[ValidateSet("Binary", "DWord", "ExpandString", "MultiString", "None", "QWord", "String", "Unknown")] [String] $valueKind = "String"
)
([Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment", $true)).SetValue($Name, $Value, $valueKind)
}
function Remove-Environment {
param (
[parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String] $Name,
[Switch] $throwOnMissingValue = $false
)
([Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment", $true)).DeleteValue($Name, $throwOnMissingValue)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment