How does persistence work?

How do i set a variable in one script, and access that same variable from another script?
It needs to be stored per user session, so another user should be able to set different content.

I enabled persistent runspace on the integrated environment which i use, but the variable i set didn’t persist across two scripts i ran back to back.

When using the $Session scope, i get this error when trying to output the variable:

[error] Object reference not set to an instance of an object.

Product: PowerShell Universal
Version: 1.4.6

I think you’ll need to look at the cache scope:

$Session is exactly that, it’s the scope of the current session, so wont be available to another script in a different session, even if you’re using the same environment.

There’s also some information here on variables:

you can also interact with those directly and create variables from your scripts

Well my assumption was that the presistence setting will keep it using one session for the environment accross all scripts. What else does the “Persistent Runspace” for an environment exactly do in powershell universal?

And the cache won’t allow to have persistent variables for seperate users, it’s one scope for all users if i got that right…

Is there no way to have a persistent scope for seperate users, with seperate content?

Session, Cache and Page scope do not work in scripts.

We currently do not have a per user variable scope for scripts. You could use New-PSUVariable to create scripts with dynamic names like $User_MyVariable and then look them up later.

You could also just use some sort of out of the box persistence like SQL or the file system.

1 Like


my bad, as i didn’t clearify this, i am actually looking to work with apps.
I just used scripts to figure out the basics, i didn’t realize that they work different in regards to these scopes.

From your videos i gathered that the session scope is supposed to do exactly what i need in apps, and that appears to work fine.