How-to set Environment variables in PS7 MacOS

Welcome Forums General PowerShell Q&A How-to set Environment variables in PS7 MacOS

Viewing 5 reply threads
  • Author
    Posts
    • #239288
      Participant
      Topics: 5
      Replies: 15
      Points: 128
      Rank: Participant

      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? I’ve run into some blog posts that mention there might be a json document somewhere with these preferences.

      How are others doing it?

      $Env:PATH  $Env:PSModulePath etc

      • This topic was modified 1 week, 1 day ago by Phatmandrake.
    • #240110
      Senior Moderator
      Topics: 9
      Replies: 1236
      Points: 4,443
      Helping Hand
      Rank: Community Hero
    • #241043
      Participant
      Topics: 5
      Replies: 15
      Points: 128
      Rank: Participant

      Thanks for sharing that. I am unable to locate that file so perhaps there is another source that is modifying $ENV:Path. Does anyone know how $PSProfile, this config file, and the existing ENV variables from MacOS Terminal interact? Trying to come up with the most stable way to modify $ENV:Path cross-platform for a module.

      • This reply was modified 22 hours, 10 minutes ago by Phatmandrake.
      • This reply was modified 22 hours, 9 minutes ago by Phatmandrake.
    • #241124
      Participant
      Topics: 10
      Replies: 120
      Points: 665
      Helping Hand
      Rank: Major Contributor

      Trying to come up with the most stable way to modify $ENV:Path cross-platform for a module.

      If I understand you’re objective correctly, I would only modify $ENV:PATH temporarily when loading the module and here’s why:

      1. The change to $ENV:PATH ships with the module
      2. You don’t have to necessarily depend on editing $PSProfile or configuration files on any target systems (and knowing Microsoft, the path or file name may change at some point)
      3. Avoid a permanent change to paths when only one module may require it
      4. Always best to leave PowerShell out-of-box config untouched for the most part

      In short, I would:

      1. Modify $ENV:PATH in:
        1. Module .psm1 if only (this) module requires it (best for shipping to other systems); or
        2. $PSProfile to be available without having to load the module
      2. Import module in $PSProfile

      I’ve accomplished something similar but for aliases to commands I’ve written that allow me to control lighting and timers from macOS Terminal.

    • #241190
      Participant
      Topics: 5
      Replies: 15
      Points: 128
      Rank: Participant

      Thank You for responding! The module depends on a Binary. It’s actually this one: https://github.com/GoogleCloudPlatform/google-cloud-powershell it’s unfortunately not being curated, and there’s an issue where the Module is unable to see the gcloud python binary that it uses to bootstrap the PS Module. It’s using a Windows only method for updating the path. It would make sense for this use case that  gcloud is made available in the $env:PATH.

      Now there are ways of updating this to not need the gcloud binary entirely, but to get this thing up and running now I wanted to make an easy to edit suggestion.

      Details: https://github.com/GoogleCloudPlatform/google-cloud-powershell/issues/643

      • This reply was modified 12 hours, 6 minutes ago by Phatmandrake.
      • This reply was modified 12 hours, 6 minutes ago by Phatmandrake.
      • This reply was modified 8 hours, 59 minutes ago by Phatmandrake.
    • #241199
      Participant
      Topics: 0
      Replies: 5
      Points: 22
      Rank: Member

      When I am programming my modules for cross platform I have done specific checks for IsMacOs or IsLinux and then changed the paths that way. I also use now PSFramework where I can store them in a central config and recall them from there which makes life easier.

      For mac you can open the Terminal and run the command printenv.For displaying the value of any specific environment variable run the echo $[variable name].You can set a temporary variable by opening the terminal and running export command followed by the variable name and its value: Daves-MacBook-Pro:~ DaveG$ export GITHUB_HOME=/Library/GitHub/.

      Mac uses bash shell. The environment variables can be added to the .bash_profile directory, for the current user. The path to this file can be found using the command: Daves-MacBook-Pro:~ DaveG$ ~/.bash_profile.

      Just use a text editor to edit the file and if no file exists then you can create a new file called .bash_profile. Edit the file and add your desired environment variables using export command. Save it and restart terminal and you should be good to go.

      • This reply was modified 11 hours, 4 minutes ago by Dave G.
Viewing 5 reply threads
  • You must be logged in to reply to this topic.