Permanent PowerShell variable

GwFiles picture GwFiles · Jan 13, 2012 · Viewed 10.3k times · Source

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.

Answer

mjsr picture mjsr · Jan 13, 2012

To store:

$variable|export-clixml -path $Location

To retrieve:

$variable = import-clixml -path $Location

Put that in a function if you want it, something like:

function LoadTHEvariable($location)
{
    $global:variable = import-clixml -path $Location
}

$location obviously contains the place in the filesystem where do you want to store the variable.