Tired of digging through hash tables? Discover a trick that lets you access config settings directly as variables and make your scripts a breeze to read.

Improve access with hash table to variable conversion

In the previous tip we used external configuration data stored in a *.psd1 file and imported it into a script using Import?PowerShellDataFile. That worked great, but Import?PowerShellDataFile always returns a hash table, so the script would always need to address the hash table first.

Lets try and improve this. Here is the *.psd1 file again:

@{

   # paths to important folders

   dataInPath = 'c:\data1', '\\server2\public\data'

   dataOutPath = '\\server99\public\results'

   dataLogPath = 'f:\local\log'



   # AD groups 

   dataGroups = 'Technicians', 'Testers', 'Auditors'



   # miscellaneaous settings
   dataTimeoutSeconds = 5400

   dataLogLevel = 4

And this is the script used in the previous tip (make sure both files are stored in the same folder):

# data.psd1 must be located in the same folder

$dataPath = Join-Path -Path $PSScriptRoot -ChildPath data.psd1



# safely read psd1 file (caveat: unsafe reads can execute code within psd1 file)

$dataHashtable = Import-PowerShellDataFile -Path $dataPath 



# output entire hash table

$dataHashtable | Out-GridView -Title 'Review of all imported data from the psd1 file'



# utilize settings from hash table

$logLevel = $dataHashtable.datalogLevel

"Current Log Level = $logLevel"



$inPath = $dataHashtable.dataInPath

foreach ($item in $inPath)

{

   "Checking $item"

Lets now add a simple filter function that takes (any) hash table, and turns each key into a variable. Here is such a simple filter:

filter Convert-HashtableToVariable

{

   foreach ($key in $_.Keys) { Set-Variable -Name $key -Value $_[$key] -Scope script }

With this filter, the script above can be greatly simplified: simply by adding Convert?HashtableToVariable, the hash table that was read from the *.psd1 file is auto-converted into script-global variables that can be immediately used. No need anymore for picking the required information from inside a hash table each time you need it:

filter Convert-HashtableToVariable

{

   foreach ($key in $_.Keys) { Set-Variable -Name $key -Value $_[$key] -Scope script }

}



# data.psd1 must be located in the same folder

$dataPath = Join-Path -Path $PSScriptRoot -ChildPath data.psd1



# convert hash table keys into variables

Import-PowerShellDataFile -Path $dataPath | Convert-HashtableToVariable









# script can now use all hash table keys like variables

# utilize settings from hash table

"Current Log Level = $datalogLevel"



foreach ($item in $dataInPath)

{

   "Checking $item"

}

All four tips around the topic 'utilizing external config data':

Each tip progressively builds on creating a more manageable, efficient, and secure way to handle configuration data in PowerShell scripts, moving from basic hash tables to more advanced type-safe objects.

Your ultimate PowerShell Cheat Sheet

Unleash the full potential of PowerShell with our handy poster. Whether you're a beginner or a seasoned pro, this cheat sheet is designed to be your go-to resource for the most important and commonly used cmdlets.The poster is available for download and in paper form.

Get your poster here!

Related links