Background (OS: Windows 10):
I have Powershell script that runs(in background) as
SYSTEM
Through Powershell script I need to set both Machine and Logged-in User Environment Variables
Machine Variables are set using
[Environment]::SetEnvironmentVariable('NAME', 'Value', 'Machine')
and works as-expectedUser Variables set using
[Environment]::SetEnvironmentVariable('NAME1', 'Value1', 'User')
does not work for End user accounts as the process runs as SYSTEMPowershell script execution needs at least one user logged in and this is working fine
I am able to find the logged in user with:
$current_user = (Get-WmiObject -Class Win32_ComputerSystem).UserName.Split('\')[1]
I tried using Registry update to set logged-in User Variables but this does not work as expected:
Set-ItemProperty -Path 'HKCU:\Environment' -Name 'NAME2' -Value 'Value2' -Force
How can I set Logged-In User Env Variable from powershell script running as SYSTEM?