Pages

Wednesday, 16 February 2022

Set user environmental variable through system contest

 # get the domain and username for the currently logged on user

 $domain, $userName = (Get-WmiObject -Class Win32_ComputerSystem).UserName -split '\\', 2

 # next, get the SID for that current user
 
 $user = [System.Security.Principal.NTAccount]::new($domain, $userName)
 
 $sid  = $user.Translate([System.Security.Principal.SecurityIdentifier]).Value
 
 # set the registry value for this user.
 Set-ItemProperty -Path "Registry::HKEY_USERS\$sid\Environment" -Name 'NAME2' -Value 'Value2' -Type String
 

No comments:

Post a Comment

Test

   $AppList = Get-InstalledApplication -Name 'Air Explorer'                 ForEach ($App in $AppList)         {         If($App.Uni...

Search This Blog