Public/Get-SpecEnvironmentVariable.ps1

Function Get-SpecEnvironmentVariable {
    <#
    .SYNOPSIS
        Retrieves the value of the specified environment variable.
 
    .DESCRIPTION
        The Get-SpecEnvironmentVariable function retrieves the value of the specified environment variable
        based on the provided variable name and scope (User, Machine, or Process). If the variable does not exist,
        it returns $false.
 
    .PARAMETER VariableName
        Specifies the name of the environment variable to retrieve.
 
    .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
        Get-SpecEnvironmentVariable -VariableName "Path" -Scope "Machine"
        Retrieves the value of the "Path" environment variable at the machine level.
 
    .EXAMPLE
        Get-SpecEnvironmentVariable -VariableName "TEMP" -Scope "User"
        Retrieves the value of the "TEMP" environment variable at the user level.
 
    .OUTPUTS
        System.String or $false.
        Returns the value of the specified environment variable if it exists. If the variable does not exist, returns $false.
 
    .NOTES
        Author :owen.heaume
        Version : 1.0
    #>



    [CmdletBinding()]

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

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

    $result = [System.Environment]::GetEnvironmentVariable($variableName, [System.EnvironmentVariableTarget]::$scope)

    if ($NULL -eq $result) {
        Write-Verbose "The variable [$VariableName] does not exist."
        return $false
    } else {
        # return the variable value
        Write-Verbose "The variable [$VariableName] exists and has a value of $result"
        return $result
    }
}