Permanent PowerShell variable
Asked Answered
V

7

8

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.

Vidette answered 13/1, 2012 at 17:40 Comment(2)
What on earth are you storing in variables that you would need to persist forever?Maisel
Well, I have a variable that has a listing of files under a directory. For example, I have a variable called $directorysnapshot = dir -r 'path' that has a listing of all files under that directory and I want to keep it the same listing. I can't have that variable redefine each time during startup because some files may have changed since thenVidette
S
2

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

Scofflaw answered 13/1, 2012 at 17:52 Comment(2)
Well, I have a variable that has a listing of files under a directory. For example, I have a variable called $directorysnapshot = dir -r 'path' that has a listing of all files under that directory and I want to keep it the same listing. I can't have that variable redefine each time during startup because some files may have changed since then.Vidette
@Vidette if it needs to be persisted though why is the registry a bad place? The result could be persisted and reloaded on startupScofflaw
H
9

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.

Hospice answered 13/1, 2012 at 18:41 Comment(0)
G
6

You could store your data in your PowerShell Profile.

Giliana answered 13/1, 2012 at 17:51 Comment(2)
This is what I do, typically. I tend to store things like svn urls and the like, so I can do merges easily, e.g. svn merge -c 100 $someBranchNameWende
Well, I have a variable that has a listing of files under a directory. For example, I have a variable called $directorysnapshot = dir -r 'path' that has a listing of all files under that directory and I want to keep it the same listing. I can't have that variable redefine each time during startup because some files may have changed since then.Vidette
I
4

Consider using an environment variable.

Iodoform answered 14/1, 2012 at 10:28 Comment(2)
Note: Environment variables do not persist across reboots unless set with SETX.Pietrek
You can set persistent environment variables with powershell using [Environment]::SetEnvironmentVariable()Flintlock
S
2

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

Scofflaw answered 13/1, 2012 at 17:52 Comment(2)
Well, I have a variable that has a listing of files under a directory. For example, I have a variable called $directorysnapshot = dir -r 'path' that has a listing of all files under that directory and I want to keep it the same listing. I can't have that variable redefine each time during startup because some files may have changed since then.Vidette
@Vidette if it needs to be persisted though why is the registry a bad place? The result could be persisted and reloaded on startupScofflaw
W
2

A variable is a container and you can create tell powershell that you want that container to be a file. ${c:\variableToKeep.txt} = 'I want to keep this value'

Everytime you want to get the value inside just call the variable ${c:\variableToKeep.txt}

You can put this file in a share folder or copy it between machines, and you can read and write that variable on any machine.

Wrought answered 28/12, 2018 at 1:22 Comment(0)
F
1

To create the variable in every PowerShell session that you start, add the variable to your PowerShell profile. You can add this command to your PowerShell profile by opening the $PROFILE file in a text editor, such as notepad.exe. (If the file doesn't exist, create it where the variable says it should be.)

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_variables?source=recommendations&view=powershell-7.2#saving-variables

Friedrich answered 28/9, 2022 at 12:55 Comment(0)
L
0

The Add-Content cmdlet has you covered. The star here is the powershell profile, accessed as $profile[.host] which is a pre-defined variable to the path where state information for the user is(permanently) stored in a rom file. The -Force at the end is key so as to cover the case where the $profile variable is not created yet.

Add-Content -Path $profile -Value $directorysnapshot -Force
Lituus answered 25/12, 2022 at 0:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.