Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a specific user environment variable on windows server

Is there a way to access to a specific user environment variable using Powershell ?

On the server machine, we have 50 users. I want to know if a specific user for example user1 is using a different value for the environment variable PYTH_HOME_LOG.

This System variable points to C:\PYTHON\LOG, I want to check which user changed this location by configuring a user environment variable PYTH_HOME_LOG.

like image 438
Linus Avatar asked Aug 23 '26 19:08

Linus


1 Answers

If you are a member of the local Administrators group you can simply go through all the user registry hives and search for the Environment variables there:

$AllUserHives = Get-ChildItem Registry::HKEY_USERS\ |Where Name -like "HKEY_USERS\S-1-5-21*[0-9]"
foreach($UserHive in $AllUserHives){
  if(Get-ItemProperty -Path "$UserHive\Environment" -Name PYTH_HOME_LOG -EA SilentlyContinue){
    # User is overriding %PYTH_HOME_LOG%
  }
}

You can grab the corresponding user name either from the Volatile Environment registry subtree by searching for the USERNAME env variable, or you can use IdentityReference.Translate():

$SID = [System.Security.Principal.SecurityIdentifier]"S-1-5-21-436246-18267386-368368356-356777"
$Username = $SID.Translate([System.Security.Principal.NTAccount]).Value

or, in this case:

$SID = ($UserHive -split '\\')[-1] -as [System.Security.Principal.SecurityIdentifier]
$Username = $SID.Translate([System.Security.Principal.NTAccount]).Value
like image 93
Mathias R. Jessen Avatar answered Aug 25 '26 14:08

Mathias R. Jessen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!