variablespowershellpermanent

Permanent PowerShell variable


Is there a way to define a variable in PowerShell so when I open up a new PowerShell window, it'll keep the same value?

I'll need this variable to keep its value, because I'll be needing to reboot my server every now and then, and I don't want to lose these values.


Solution

  • Have you considered other alternate sources for storing the variable? Variables in PowerShell are generally meant to persist only as long as the PowerShell session itself. However there are several other sources that PowerShell can easily query that are meant to persist longer. In particular the registry and file system.

    For a variable meant to persist across reboots I would store it in the registry and then use PowerShell to query that value (perhaps cache in a session variable).