To refresh the environment variables in PowerShell, retrieves the environment variables and assign them $Env:Path to reload the environment variable path in the PowerShell. After reloading the path in PowerShell, you don’t need to restart the PowerShell ISE or terminal.
The Path environment variable specify a set of directories where the executable programs are located. Using the $Env:Path
, you can get a list of paths.
In this application, we will discuss how to refresh environment variables or reload the path variable without restarting PowerShell.
Refresh Environment Variables in PowerShell
To reload the path environment variable using PowerShell, run the following command.
$Env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
In the above PowerShell script, we use .Net Framework class System.Environment
and its method GetEnvironmentVariable()
to get Machine Path.
GetEnvironmentVariable() method retrieves the environment variables from the current process. In the above script, it gets the Path environment variable and assigns it to $Env:Path.
Using the above script, it gets the modified Path environment variables and assigns them to the Path environment variable to use in the current process.
It reloads the environment variable, hence you don’t need to restart the PowerShell.
Cool Tip: How to print environment variables in PowerShell!
You can also reload the user environment variable along with the Path variable using the following PowerShell script.
$Env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
In the above PowerShell script, using the GetEnvironmentVariable method, it retrieves the Path environment variable and user environment variables.
It joins both machine and user environment variables and assigns them to $Env.Path environment variables to ensure using the updated environment variables in the current session.
Cool Tip: How to set environment variables in PowerShell!
Conclusion
I hope the above article on how to refresh the environment variable in PowerShell is helpful to you.
The reloading of the environment path is required if the path environment variable is modified because of application installation or for any reason and you want to use the updated path variable.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.