Is there a way to define a variable in PowerShell, so when I open a new PowerShell window it will keep the same value?
I need this variable to save its value, because I will have to periodically restart my server, and I do not want to lose these values.
Have you considered other alternative sources for storing a variable? Variables in PowerShell are usually saved only until the PowerShell session itself. However, there are several other sources that PowerShell can easily request for longer. In particular, the registry and file system.
, , , PowerShell (, ).
:
$variable|export-clixml -path $Location
$variable = import-clixml -path $Location
, , - :
function LoadTHEvariable($location) { $global:variable = import-clixml -path $Location }
$location, , , .
You can save your data in your PowerShell Profile .
Consider using an environment variable.