Reading Data from .PSD1 Files in PowerShell 5+

by Nov 24, 2017

In a previous tip we introduced Import-LocalizedData to read in data stored in a .psd1 file.

Starting in PowerShell 5, there is a new cmdlet named Import-PowerShellDataFile. You can use it to safely read data from a .psd1 file. Like Import-LocalizedData, this cmdlet accepts only .psd1 files with no active content (no commands or variables).

Here is the script you need:

$path = "$PSScriptRoot\data.psd1" $infos = Import-PowerShellDataFile -Path $path 

Store the data file in the same folder, name it data.psd1, and assign this content:

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

When you run the script, it returns the data from the .psd1 file as a hash table.

Twitter This Tip! ReTweet this Tip!