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 Exchange
Discussions

Set a persistent environment variable on a windows 10 computer with PowerShell?
Check out this method: https://docs.microsoft.com/en-us/dotnet/api/system.environment.setenvironmentvariable?view=net-6.0#system-environment-setenvironmentvariable(system-string-system-string-system-environmentvariabletarget) More on reddit.com
🌐 r/PowerShell
12
3
February 3, 2022
powershell - permanent environment variables
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 ... More on community.spiceworks.com
🌐 community.spiceworks.com
8
6
February 24, 2023
How-to set Environment variables in PS7 MacOS
Does anyone know if [Environment]::SetEnvironmentVariable is it a valid way to permanently set environment variables in PS 7.0.2 on MacOS? From my testing, it doesn’t appear to be a permanent operation. Is it the case that the only way to achieve this on MacOS is by adding it to your PSProfile? More on forums.powershell.org
🌐 forums.powershell.org
0
0
June 30, 2020
Permanent Variable Set-Up In Powershell
70+, gawd what's in there and are you sure you don't want to just define default parameter values instead More on reddit.com
🌐 r/PowerShell
8
6
November 30, 2022
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_environment_variables
about_Environment_Variables - PowerShell | Microsoft Learn
October 1, 2025 - Unlike the variable syntax and provider cases, assigning the value to $null using the SetEnvironmentVariable() method doesn't remove the environment variable. For more information about the methods of the System.Environment class, see Environment ...
🌐
GitHub
gist.github.com › mitchmindtree › 92c8e37fa80c8dddee5b94fc88d1288b
How to permanently set user environment variables on Windows · GitHub
Using the command prompt: setx FOO "path\to\foo" Using powershell: [Environment]::SetEnvironmentVariable("FOO", "path\to\foo", [System.EnvironmentVariableTarget]::User) Using the windows GUI.
Find elsewhere
🌐
Petri
petri.com › home › powershell set environment variable – a step-by-step guide
PowerShell Set Environment Variable - A Step-By-Step Guide | Petri
May 5, 2025 - You’ve also explored the different ... with PowerShell. By using $Env, Set-Item cmdlet, and the [System.Environment] .NET class method, you can easily set and manage environment variables. Remember, changes made to variables in different scopes have different permanence ...
🌐
Netwrix
netwrix.com › en › resources › blog › powershell-environment-variables
PowerShell Environment Variables
August 25, 2025 - If you need to create or update environment variables that persist beyond the session, you will need to use the “SetX” command, which allows you to define environment variables at the user or system level.
🌐
Sigasi
sigasi.com › knowledge › how_tos › setting-environment-variables
Setting Environment Variables - Sigasi
September 23, 2024 - Set an environment variable temporarily by typing: ... Note: This will only last for the current terminal session. The variable will disappear when the terminal is closed. Open a terminal and use a text editor to open your shell configuration file (.bashrc, .zshrc, etc. depending on your shell): ... Save the file and exit the editor. ... Note: This will make the environment variable persistent across terminal sessions.
🌐
PowerShell Forums
forums.powershell.org › powershell help
How-to set Environment variables in PS7 MacOS - PowerShell Help - PowerShell Forums
June 30, 2020 - Does anyone know if [Environment]::SetEnvironmentVariable is it a valid way to permanently set environment variables in PS 7.0.2 on MacOS? From my testing, it doesn’t appear to be a permanent operation. Is it the case t…
🌐
Configu
configu.com › home › setting environment variables in powershell: a practical guide
Setting Environment Variables in PowerShell: A Practical Guide - Configu
January 17, 2025 - This method is temporary; the modification will reset once the PowerShell session ends: # Temporarily add a new directory to the PATH environment variable $env:PATH = $env:PATH + ';C:\bin' For a more lasting modification—such as updating the PATH environment variable across all sessions—you can use the .NET method with the Machine target. This command permanently updates the system’s environment settings by adding C:\bin to the PATH variable in the machine’s registry:
🌐
Reddit
reddit.com › r/powershell › permanent variable set-up in powershell
r/PowerShell on Reddit: Permanent Variable Set-Up In Powershell
November 30, 2022 -

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.

🌐
DEV Community
dev.to › asmitbm › setting-windows-powershell-environment-variables-2glb
Setting Windows PowerShell environment variables - DEV Community
March 7, 2022 - So, I was assigned a task in one of the Open-source project named MetaCall, a super cool tool used for Polyglot programming, where I had to add a path to PATH variable permanently, so even if PowerShell session was over, the path value still stayed in PATH variable, basically to persist it. This is what I came up with after hours of debugging (as I was using PowerShell for the very first time). # Test folder $InstallLocation = "C:\bin" # To add folder to PATH $persistedPath = [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::Machine) -split ';' if ($persistedPath -notc
🌐
Phidata
docs.phidata.com › faq › environment_variables
Setting Environment Variables - Phidata
To make environment variables persist across sessions, add them to your PowerShell profile script (e.g., Microsoft.PowerShell_profile.ps1). ... These environment variables will only be available in the current Command Prompt session.
🌐
Opensource.com
opensource.com › article › 19 › 9 › environment-variables-powershell
Environment variables in PowerShell | Opensource.com
Environment variables can be set, recalled, and cleared with some of the same syntax used for normal variables. Like other variables, anything you set during a session only applies to that particular session. If you want to make permanent changes to a variable, you must change them in Windows Registry on Windows, or in a shell configuration file (such as ~/.bashrc) on Linux or Mac. If you’re not familiar with using variables in PowerShell, read my variables in PowerShell article before continuing.
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › powershell › how to set environment variables in powershell?
How to Set Environment Variables in PowerShell? - SharePoint Diary
October 7, 2025 - To manage environment variables from PowerShell, You can use the [System.Environment] methods from .NET class that sets the environment variables permanently or by using the $env: in PSDrive for temporary session variables.
🌐
Twilio
twilio.com › en-us › blog › how-to-set-environment-variables-html
How to Set Environment Variables | Twilio
December 9, 2025 - If you're running your application from a shell and don't want the environment variables to stick around, set the environment variables for the current process. If you do want the environment variables to stick around, store them in the user registry. If you want to configure environment variables for all users, not just for a specific user or yourself, then store them in the machine registry. While PowerShell is also supported on macOS and Linux distributions through PowerShell Core, Windows PowerShell is preinstalled on Windows and is the recommended shell for the Windows platform.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-set-environment-variables-using-powershell
How to Set environment variables using PowerShell?
To add or set the environment variable ... need to use the .NET method. To set the environment persistently so they should remain even when close the session, PowerShell uses [System.Environment] class with the SetEnvironmentVariable method for the environment variable to set it persistently...