- This topic has 5 replies, 4 voices, and was last updated 2 months, 1 week ago by
June 30, 2020 at 3:32 pm #239288
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 2 months, 2 weeks ago by Phatmandrake.
July 3, 2020 at 3:55 pm #240110Senior ModeratorTopics: 9Replies: 1309Points: 4,781Rank: Community Hero
July 8, 2020 at 9:54 am #241043
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.
July 8, 2020 at 1:16 pm #241124ParticipantTopics: 11Replies: 127Points: 761Rank: 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:
- The change to $ENV:PATH ships with the module
- 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)
- Avoid a permanent change to paths when only one module may require it
- Always best to leave PowerShell out-of-box config untouched for the most part
In short, I would:
- Module .psm1 if only (this) module requires it (best for shipping to other systems); or
- $PSProfile to be available without having to load the module
- 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.
July 8, 2020 at 7:58 pm #241190
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.
July 8, 2020 at 9:00 pm #241199
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.
- You must be logged in to reply to this topic.