Since PowerShell is built on .NET, there is a AppDomain (I’ll go into more detail in a later post) which has a lot of information about the .NET environment (what assemblies are loaded, etc..).

One feature that the AppDomain has is to store globally accessible name/value pairs.  Normally, variables in PowerShell should handle most of your “in-process” storage needs, but for the times that they don’t, you have your AppDomain.

To access the AppDomain object, you can use the static property CurrentDomain on the System.AppDomain class.

(NOTE: Though I usually refer use the fully qualified namespace, PowerShell does allow you to skip the “System” portion of the namespace.  I’m using the full namespace for clarity on the blog, but when typing on the command line, it is easier to skip that.)

PS C:> [system.appdomain]::CurrentDomain

To save a name/value pair to your AppDomain, you can use the SetData method.

PS C:> [system.appdomain]::CurrentDomain.SetData('ThisIsMyNameOrKey', ('This','Can','Be','Any','Object','Or','Collection') )

Any object or collection can be saved in the AppDomain, and you are not limited to just one.

Accessing those values is just as easy, using the GetData method.

PS C:> [system.appdomain]::CurrentDomain.GetData('ThisIsMyNameOrKey')
This
Can
Be
Any
Object
Or
Collection

And there we have our collection back.

PSMDTAG:.Net AppDomain

Want more great reading? Check out my reading list!