Public/Set-SpecEnvironmentVariable.ps1

Function Set-SpecEnvironmentVariable {

    [CmdletBinding()]

    param (
        [Parameter(Mandatory = $true)]
        [AllowNull()]
        [AllowEmptyString()]
        [string]$VariableName,

        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [AllowNull()]
        [string]$VariableValue,

        [validateset ("User", "Machine", "Process") ]
        [string]$scope
    )

    try {
        write-verbose "Adding [$VariableName] with a value of [$VariableValue] at the $scope level"
        [System.Environment]::SetEnvironmentVariable($VariableName, $VariableValue, [System.EnvironmentVariableTarget]::$scope)
        write-verbose "Setting at scope: $scope"
        return 0
    } catch {
        Write-Warning "An error occurred while setting the variable at the $scope level: $_"
        return 2
    }
}