Skip to the main content.

Unlocking the Power of PowerShell: Tips for Success

Unleash IntelliSense in PowerShell – From hash tables to intuitive objects


Simplify your PowerShell scripts by converting hash tables into objects! With all data in one variable, you’ll unlock IntelliSense support, making configuration data easier to find and use.

Convert hash tables into objects 

In the previous tip we used external configuration data stored in a *.psd1 file and imported it into a script using Import‑PowerShellDataFile. Access to *.psd1 data was simplified by turning all hash table keys into easily accessible script-global variables.

There is another great alternative that we tackle in this tip: converting a hash table to an object. This way, you have all external information contained in one variable, and IntelliSense helps you pick the information you may need.
Let’s 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 that reads in the *.psd1 file as a hash table (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 


Hash tables can easily be converted to objects in PowerShell, so this gets you an object instead of a hash table:


# data.psd1 must be located in the same folder
$dataPath = Join-Path -Path $PSScriptRoot -ChildPath data.psd1

# convert hash table keys into variables
$hashObject = [PSCustomObject](Import-PowerShellDataFile -Path $dataPath)
$hashObject | Out-GridView -Title 'Hash table as Object'
$hashObject.dataLogLevel 


As object, all *.psd1 data is now contained in one single object, and exposed as different object properties. This simplifies coding a lot because now you get IntelliSense once you type "hashObject.", and get a list of all external properties that are present in the *.psd1 file.

 

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.

 

Good2know

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.

PowerShell Poster 2023

Get your poster here!

 

 

Related links 

Related posts

6 min read

Encoding vs encryption? Understand the difference and choose wisely!

Understanding encoding vs encryption is essential for robust data security. But what is the difference between encoding...

6 min read

Enhancing SecureString security: Best practices on different platforms

How does Windows keep SecureString safe? Dive deeper into encryption methods and uncover cross-platform security gaps...

6 min read

Cross-platform encryption risks with .NET's SecureString

Think your SecureString is safe? Not on all platforms! Discover the risks of cross-platform data encryption with ...

About the author: