1

I want to use variables over PowerShell instances away. (I know that global variables are not nice) Is this the best way to define a variable over instances in PowerShell? Other ideas? ($global:variable is not over PowerShell instances away)

[Environment]::SetEnvironmentVariable("TestVariable", "Test value.", "User")

[Environment]::GetEnvironmentVariable("TestVariable","User")

1 Answer 1

4

Yes. Other options would be to:

  • Write/read a hashtable to a settings file using Export/Import-CliXml.
  • Stash information in the user's registry hive.

But adding a user environment variable is also a good way to go and the way you suggest is what is needed for the environment changes to survive exiting the current PowerShell session.

Sign up to request clarification or add additional context in comments.

1 Comment

Yea, I would suggest using a file over using the registry, and the registry over using the environment variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.