LaPhi
LaPhi

Reputation: 5897

Best way to using Global Variables in PowerShell?

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")

Upvotes: 1

Views: 1455

Answers (1)

Keith Hill
Keith Hill

Reputation: 201692

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.

Upvotes: 4

Related Questions