On our PowerShell community forums, we have recently received questions asking if it is possible to store PowerShell variables in external files, and if so, how is it done. It is indeed possible to store variables in external files for PowerShell to use. These external variables can be stored in a variety of file types. Storing them in a PowerShell file is one of the easiest because can just dot source these files.
We will cover the following methods to store variables:
- Script Files
- Text Files
- JSON Files
- XML Files
The examples shown in this post are pretty simple, but that doesn’t mean that it isn’t possible to store fairly complex variables in external files. If you want to experiment with storing external variables you can download the sample files for this post here.
Dot sourcing may be the easiest way to store external variables—but it isn’t always the safest. When a file is dot sourced we are telling PowerShell to execute that script. If there is any malicious code in the file, then that code will also run.
In addtion to dot sourcing, you will also need to ensure that the external variables PowerShell script is signed and that Remote Execution is enabled on your machine.
Dot sourcing can be helpful if we need to get information about something dynamically. For the other options discussed in this post, the data stored in the file types will have to be manually changed.
1 2 3 4
Here is what is in the ExternalVariables.ps1 file:
#Declaration of External variables
$External_Variable1 = ‘Sapien’
$External_Variable2 = ‘Technologies’
External variables can also be stored in a number of text files and formats, such as plain text in a general .txt file using the Get-Content cmdlet. When we import variables this way, we aren’t running any code, so it’s not a problem if you don’t constantly monitor the files to get information.
The following three pictures are examples of different ways of storing information in a simple text file:
1 2 3 4 5 6 7 8 9 10 11 12 13
Here is what is in the ExternalVariables.txt file:
Just like an array, we can store hash tables in text files. To get our hash table from a text file, we will have to pipe the output of the Get-Content to the ConvertFrom-StringData cmdlet to convert the output into a hash table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Here is what is in ExternalVariablesHashTable.txt:
Storing information in a text file like this is a convenient way to keep information in a human-readable format. Text files also come with the benefit of not being executable, so if there happens to be malicious code stored in a file you don’t regularly manage it won’t be executed.
It is also possible to store external variables in a JSON format. The only caveat being that we will need to once again pipe the output of Get-Content to another cmdlet; however this time it’s ConvertFrom-Json rather than ConvertFrom-StringData. For those unfamiliar with JSON or need to brush up on the format, please visit www.JSON.org.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Here is what is in the JSON File:
If we store our variables in an XML format, we can add comments to the variable file if necessary. The only two file formats that we will talk about in this post that allows for comments are XML or PS1. JSON and normal TXT files do not allow for comments. For a concise overview of the XML format, visit w3schools.com/xml.
1 2 3 4 5 6 7 8 9
Here is what is in the XML file:
Two of our famous Belgian Waffles with plenty of real maple syrup
Strawberry Belgian Waffles
Light Belgian waffles covered with strawberries and whipped cream
Berry-Berry Belgian Waffles
Belgian waffles covered with assorted fresh berries and whipped cream
When choosing between XML and JSON storage formats, it comes down to which one is more familiar. Since the main difference between them is that XML allows comments, it is just a matter of preference. All of these options are viable ways to store information in external files to either be read by another program or used by the same program at a later time. How complex, whether it is dynamic or not, and how much information needs to be stored will dictate the format to use.
Exporting to Files
Just like importing information with PowerShell, it is also possible to export information and objects to an external file from the program we are using. PowerShell Studio 2018 comes with snippets that make exporting information much easier—simply pass the path of the external file and the object to the corresponding export function and the snippet will take care of everything else. We will cover Exporting to Files using Snippets in a future blog post.