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')
Answer from Markus Meyer on Stack ExchangeIf sometime during a PowerShell session you need to see or to temporarily modify the PATH environment variable, you can type one of these commands:
$env:Path # shows the actual content
$env:Path = 'C:\foo;' + $env:Path # attach to the beginning
$env:Path += ';C:\foo' # attach to the end
Changing the actual environment variables can be done by
using the env: namespace / drive information. For example, this
code will update the path environment variable:
$env:PATH = "SomeRandomPath"; (replaces existing path)
$env:PATH += ";SomeRandomPath" (appends to existing path)
Making change permanent
There are ways to make environment settings permanent, but if you are only using them from PowerShell, it's probably a lot better to use Powershell profiles script.
Everytime a new instance of Powershell starts, it look for specific script files (named profile files) and execute them if they do exist. You can edit one of these profile to customize your enviroment.
To know where those profile scripts are located in your computer type:
$profile
$profile.AllUsersAllHosts
$profile.AllUsersCurrentHost
$profile.CurrentUserAllHosts
$profile.CurrentUserCurrentHost
You can edit one of them, for example, by typing:
notepad $profile
Set a persistent environment variable on a windows 10 computer with PowerShell?
powershell - permanent environment variables
How-to set Environment variables in PS7 MacOS
Permanent Variable Set-Up In Powershell
Videos
I need to set a system wide environment variable and I need it to be persistent.
Every time I try setting it with PowerShell it seems to only be good for that PowerShell session.
Is there a way to do this with PowerShell?
I think you may be running up against user scope vs machine scope. I’m guessing you are setting the user scope in your command and not the machine scope.
Specify the machine scope.
learn.microsoft.com
about Environment Variables - PowerShell
Describes how to access and manage environment variables in PowerShell.
I’m trying to append some environment variables to Path and found this:
$path = $env:Path + ";valuehere"
[environment]::SetEnvironmentVariable('Path',$path)
and when I do $env:Path, it shows, but not when I open up the gui for environement variables and when I restart, powershell no longer recognizes the changes.
Any ideas?
I have 70+ permanent variables I want to setup in Powershell. I read Microsoft Docs and used Set-Item command for setting 2 permanent variables. Problem is I am not able to use it.
Like --
PS> Set-Item -Path Env:\hp_prod -Value 'hp/hello123@database1.dc1:1521/dc1.internal'
This is example of my database connection string. So, I want to use it like this to connect database:
PS> sqlplus $hp_prod
In Linux environment if I put this in "bash_profile" file then it works there. So, I want to do same in Powershell.
Kindly advice.