The Computer Oracle

how do I permanently set a system variable ([System.Environment]::SetEnvironmentVariable) in windows 11?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities

--

Chapters
00:00 How Do I Permanently Set A System Variable ([System.Environment]::Setenvironmentvariable) In Windows
00:28 Accepted Answer Score 35
01:00 Thank you

--

Full question
https://superuser.com/questions/1729958/...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#powershell

#avk47



ACCEPTED ANSWER

Score 35


Please specify the scope when setting the variable:

Saving environment variables with SetEnvironmentVariable

On Windows, you can specify a scope for the SetEnvironmentVariable method as the third parameter to set the environment variable in that scope. The machine and user scopes both persist outside of the current process, allowing you to save a new or changed environment variable.

[System.Environment]::SetEnvironmentVariable('ResourceGroup','AZ_Resource_Group', 'Machine')
[System.Environment]::SetEnvironmentVariable('ResourceGroup','AZ_Resource_Group', 'User')