Public/Remove-SpecEnvironmentVariable.ps1
Function Remove-SpecEnvironmentVariable { <# .SYNOPSIS Removes the specified environment variable at the specified scope (User, Machine, or Process). .DESCRIPTION The Remove-SpecEnvironmentVariable function removes the specified environment variable at the specified scope (User, Machine, or Process). It sets the variable to $null, effectively deleting it. .PARAMETER VariableName Specifies the name of the environment variable to remove. .PARAMETER Scope Specifies the scope of the environment variable. Valid values are "User" (user-level variable), "Machine" (system-level variable), or "Process" (variable available only to the current process). .EXAMPLE Remove-SpecEnvironmentVariable -VariableName "SampleVar" -Scope "Machine" Removes the environment variable named "SampleVar" at the machine level. .EXAMPLE Remove-SpecEnvironmentVariable -VariableName "TEMP" -Scope "User" Removes the environment variable named "TEMP" at the user level. .OUTPUTS Integer. Returns 0 if the removal is successful. Returns 2 if an error occurs while setting the environment variable. .NOTES Author : owen.heaume Prerequisite : 1.0 #> [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [AllowNull()] [AllowEmptyString()] [string]$VariableName, [validateset ("User", "Machine", "Process") ] [string]$scope ) try { write-verbose "Adding [$VariableName] with a value of [$VariableValue] at the machine level" [System.Environment]::SetEnvironmentVariable($VariableName, $NULL, [System.EnvironmentVariableTarget]::$scope) write-verbose "Removing at scope: $scope" return 0 } catch { Write-Warning "An error occurred while setting the machine level environment variable: $_" return 2 } } |