Reading Data from .PSD1 Files

by Nov 23, 2017

There are many ways how a script can store data information. One is especially convenient. Here is the code:

Import-LocalizedData -BaseDirectory $PSScriptRoot -FileName data.psd1 -BindingVariable Info $Info 

Make sure you save the code in a script. Then, create another file in the same folder, and name it “data.psd1”, and add this content:

@{ Name = 'Tobias' ID = 12 Path = 'c:\Windows' } 

With both files in place, run the script. It reads data.psd1 and returns its content as a hash table. Note that Import-LocalizedData by default does not accept .psd1 files with active content. When your hash table in data.psd1 contains commands or variables, it no longer can be read – to prevent hackers from smuggling active content into your data files.

If you add subfolders to your folder and assign culture IDs as names, for example “de-de”, and “en-us”, Import-LocalizedData will automatically look into the appropriate subfolder and read the file from there (provided you save localized copies of your data in these folders). The cmdlet either takes the culture found in $PSCulture, or you can use the parameter -UICulture.

Twitter This Tip! ReTweet this Tip!