Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Environemnt Type to reg_expand_sz using powershell

I am stuck on a issue. I am trying to set user-environment variables using PowerShell:

[Environment]::SetEnvironmentVariable($key,$value,'User')

But when I look into the registry I can see the new entry is of type REG_SZ.

I would like it to be of type REG_EXPAND_SZ.

Can someone help me with this problem?

like image 838
ajax Avatar asked Jan 16 '23 05:01

ajax


1 Answers

Use the Microsoft.Win32 namespace for Registry methods. To set the data type, use RegistryValueKind enumeration. Like so:

[Microsoft.Win32.Registry]::SetValue("HKEY_CURRENT_USER\Environment","MyKey","1",[Microsoft.Win32.RegistryValueKind]::ExpandString)

User variables are located in HKEY_CURRENT_USER\Environment

System variables are located in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

like image 122
vonPryz Avatar answered Feb 16 '23 04:02

vonPryz