2

Using Windows (e.g. 10 but I hope it doesn't matter), is there any kind of environment variable whose life ends with the user session in which the variable was created?

I'm talking about something a simple program (e.g. a .NET one) can use to store a value before shutting down, so that if the program is started again before the user logs off Windows, that value can be recovered; if the user logs off Windows, logs in and starts the said program, the variable won't be available anymore.

Requirements:

  • I'm looking for an OS feature, not something I'll have to manage directly except creating it when the program is started in a new Windows session
  • A file based mechanism will do too
  • I don't want the mentioned program to lurk in memory and hold the variable, I want to kill the program and still have the variable inside Windows
  • If the variable is encrypted, much better
2
  • 1
    Have you studied variables? There are indeed session variables, and they are well documented. What research have you done? What have you tried? Commented Mar 23, 2021 at 14:25
  • @music2myear I googled "Windows user session variables", "Windows session variables", "Windows temporary variables" but I couldn't find anything fitting my requirements. Should I have searched anything else? Were there any other names I didn't think of? Commented Mar 23, 2021 at 14:48

2 Answers 2

2

The registry key "HKCU\Volatile Environment" seem to do what you want, although I have not found any documentation for this functionality.

I tested the following (in an Administrator CMD, Windows 10):

> reg query "HKCU\Volatile Environment" /v MYVOLATILEVAR
ERROR: The system was unable to find the specified registry key or value.

> reg add "HKCU\Volatile Environment" /v MYVOLATILEVAR /t REG_SZ /d avalue
The operation completed successfully.

> reg query "HKCU\Volatile Environment" /v MYVOLATILEVAR
HKEY_CURRENT_USER\Volatile Environment
    MYVOLATILEVAR    REG_SZ    avalue

The value remains after cmd exit and screen lock, but goes away after logout.

The reg command: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/reg

The idea to test key "HKCU\Volatile Environment" was found in a comment in the page https://ss64.com/nt/syntax-variables.html

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Apr 25, 2023 at 13:50
1

The only way I see to implement your requirements is to set the permanent environment variable during logon, then undo it during logoff.

You could write scripts to run during logon and logoff that use the setx command.

References:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .