Created
August 27, 2018 04:05
-
-
Save alphp/40ee173d4bbde03fc872489f7e07c946 to your computer and use it in GitHub Desktop.
PowerShell module for Get/Set/Remove environment variables in Windows
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-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