Powershell: Modify Environment Variables (for example $env:PSModulePath) on Linux

Created on 25 Jul 2017  ·  3Comments  ·  Source: PowerShell/PowerShell

Hi all,

I think it would be great if you can change Environment Variables (accessed for example by $env:Name or Get-ChildItem env:) on Linux.

For example to set there values a new configuration file in /etc ?

On Windows I can change for example PSModulePath and add a custom path. The only option on Linux is to create a profile ps1 file for all users.

Thank you.

Issue-Discussion Issue-Enhancement WG-Engine-Providers

Most helpful comment

Can I suggest that you change the title of your issue to reflect the fact that you're looking for PowerShell to introduce a _profile-independent_ way to _persistently_ define environment variables?

A few thoughts:

  • Environment variables are by definition _shell/application-agnostic_ and are seen by _all_ descendant processes, irrespective of what shell/application created them.

  • PowerShell "listens to" only _2_ environment variables, $env:PSModulePath being one of them.

  • From what I understand, Linux environments (all of them?) already have file /etc/environment as a system-wide, shell-independent mechanism for defining environment variables.

  • That said, if a set of environment variables is truly only used by _PowerShell_, then the system-wide _PowerShell_ profile - $profile.AllUsersAllHosts - arguably _is_ the right place to define them.

    • This solution would also work for macOS, which doesn't have a system-wide mechanism for defining environment variables (anymore).
    • (macOS has /etc/profile, which the default shell is expected to process, which (a) doesn't take effect for GUI applications, and (b) requires the default shell to honor this expectation and (c) requires the default shell to understand POSIX-shell syntax (which PowerShell, if it were defined as the default shell, wouldn't [_Update_: /etc/profile and ~/.profile now _are_ honored if you start with pwsh -l or use PowerShell as the default shell]).

All 3 comments

Can I suggest that you change the title of your issue to reflect the fact that you're looking for PowerShell to introduce a _profile-independent_ way to _persistently_ define environment variables?

A few thoughts:

  • Environment variables are by definition _shell/application-agnostic_ and are seen by _all_ descendant processes, irrespective of what shell/application created them.

  • PowerShell "listens to" only _2_ environment variables, $env:PSModulePath being one of them.

  • From what I understand, Linux environments (all of them?) already have file /etc/environment as a system-wide, shell-independent mechanism for defining environment variables.

  • That said, if a set of environment variables is truly only used by _PowerShell_, then the system-wide _PowerShell_ profile - $profile.AllUsersAllHosts - arguably _is_ the right place to define them.

    • This solution would also work for macOS, which doesn't have a system-wide mechanism for defining environment variables (anymore).
    • (macOS has /etc/profile, which the default shell is expected to process, which (a) doesn't take effect for GUI applications, and (b) requires the default shell to honor this expectation and (c) requires the default shell to understand POSIX-shell syntax (which PowerShell, if it were defined as the default shell, wouldn't [_Update_: /etc/profile and ~/.profile now _are_ honored if you start with pwsh -l or use PowerShell as the default shell]).

I tried setting an environment variable like this on an Ubuntu machine:
I was hoping this would edit the /etc/environment file to set the environment variable system wide.

Unfortunately .NET Core have yet to implement a method to set environment variables in Unix systems. The documentation for those methods states that applying values to anything other than the current process is only supported on windows. :frowning_face:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JohnLBevan picture JohnLBevan  ·  3Comments

alx9r picture alx9r  ·  3Comments

Michal-Ziemba picture Michal-Ziemba  ·  3Comments

SteveL-MSFT picture SteveL-MSFT  ·  3Comments

MaximoTrinidad picture MaximoTrinidad  ·  3Comments