Jump to content

EnvGet returns user and system environment variables concurrently


Travis
 Share

Recommended Posts

I am on WIndows 10.

If you have a system and user environment variable of the same name, in my case, PATH, when calling

EnvGet("PATH")

The return is the system PATH with the user PATH at the end. No indicator of which ends where.

Is it not possible to specify the scope of the variable I am trying to access?

Link to comment
Share on other sites

1 hour ago, Travis said:

Is it not possible to specify the scope of the variable I am trying to access?

You can query the current user's environment variables or the system's environment variables by reading the appropriate registry key:

  • HKEY_CURRENT_USER\Environment
  • HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

Example:

$sRegValue = RegRead("HKEY_CURRENT_USER\Environment", "Path")
ConsoleWrite("Current User:" & @CRLF & $sRegValue & @CRLF)

$sRegValue = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "Path")
ConsoleWrite("System:" & @CRLF & $sRegValue & @CRLF)

 

Edited by TheXman
Link to comment
Share on other sites

Here is part of a environment variable udf that I started working on.

ConsoleWrite("System Path = " & _EnvVarGet("Path", 2) & @CRLF & @CRLF)
ConsoleWrite("User Path = " & _EnvVarGet("Path", 3) & @CRLF & @CRLF)


; #FUNCTION# ====================================================================================================================
; Name ..........: _EnvVarGet
; Description ...: Get Environment Variables
; Syntax ........: _EnvVarGet($_sEnvVarGet[, $_iEnvVarType = 3[, $_bExpVarValue = False]])
; Parameters ....: $_sEnvVarGet         - Environment variable name
;                : $_iEnvVarType        - [optional] Environment variable type. Default is 3.
;                :  - 0 - Returns Enviroment variable from all profile types
;                :  - 1 - Process Enviornment Variable 
;                :  - 2 - System Enviornment Variable
;                :  - 3 - User Enviornment Variable (default)
;                :  - 4 - Volatile Enviornment Variable
;                : $_bExpVarValue       - [optional] Expand Environment Variables within result. Default is False.
; Return values .:
;                : Flag: $_iEnvVarType
;                :  - 0 - Returns Enviroment variable from all profile types as an array
;                :  - 1 - Process Enviornment Variable as a string
;                :  - 2 - System Enviornment Variable as a string
;                :  - 3 - User Enviornment Variable as a string
;                :  - 4 - Volatile Enviornment Variable as a string
; Author ........: Subz
; Modified ......: 
; Remarks .......: 
; Related .......: 
; Link ..........: 
; Example .......: No
; ===============================================================================================================================
Func _EnvVarGet($_sEnvVarGet, $_iEnvVarType = 3, $_bExpVarValue = False)
    Local $iExpandEnvStrings = Opt("ExpandEnvStrings")
    Local $sEnvVarValue, $aEnvVarValue[5] = [4]
    Local $oEnvVarType, $aEnvVarType[5] = [4, "PROCESS", "SYSTEM", "USER", "VOLATILE"]
    Local $oWshShell = ObjCreate("WScript.Shell")
    Switch $_iEnvVarType
        Case 0
            If $_bExpVarValue == True Then Opt("ExpandEnvStrings", 1)
            For $i = 1 To $aEnvVarType[0]
                $oEnvVarType = $oWshShell.Environment($aEnvVarType[$i])
                $aEnvVarValue[$i] = $oEnvVarType($_sEnvVarGet)
            Next
            Opt("ExpandEnvStrings", $iExpandEnvStrings)
            Return $aEnvVarValue
        Case 1 To 4
            If $_bExpVarValue == True Then Opt("ExpandEnvStrings", 1)
            $oEnvVarType = $oWshShell.Environment($aEnvVarType[$_iEnvVarType])
            $sEnvVarValue = $oEnvVarType($_sEnvVarGet)
            Opt("ExpandEnvStrings", $iExpandEnvStrings)
            Return $sEnvVarValue
        Case Else
            Return SetError(1, 0, "")
    EndSwitch
EndFunc

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...