3 min read
PowerShell Array Caveats: Common Issues and Solutions
Here is part two of our tips on PowerShell arrays.
Let’s explore some common caveats with arrays, starting with the...
Unlocking the Power of PowerShell: Tips for Success
Say goodbye to hardcoding paths and settings! Learn how to simplify your PowerShell scripts by storing config data separately with hash tables in *.psd1 files.
Often, scripts require configuration information that may change, i.e. paths to relevant folders, group names, and more.
It is always advisable to separate such information from code, and one simple approach is to use *.psd1 files. In a mini-series, we’ll look at powerful yet simple techniques that allow you to keep configuration data separated from your code.
Let’s take a look at the official PowerShell data file format: *.psd1 files always contain exactly one hash table structure, i.e.:
@{
# 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
}
Note: you do not need to prepend hash table keys with a prefix like “data”, however such a prefix can later help easily identify information that you imported from an external *.psd1 file.
Hash tables support all relevant PowerShell data types, can define arrays (lists), and can also be commented for better maintenance.
There are various cmdlets and .NET APIs to read in *.psd1 content, and choosing the correct technique is crucial: since *.psd1 files may contain active commands, reading *.ps1 files with unsafe commands can accidentally invoke embedded code.
That’s why you should use cmdlets focusing on safe reading, like Import-PowerShellDataFile.
If you save above hash table to a file named data.psd1 and place this file next to the script below, the script would safely read the data from the *.psd1 file and expose it as a hash table (make sure you also save the script to a file – do not run "untitled" scripts):
# 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 hashtable
$dataHashtable | Out-GridView -Title 'Review of all imported data from psd1 file'
# utilize settings from the hash table
$logLevel = $dataHashtable['datalogLevel']
"Current Log Level = $logLevel"
$inPath = $dataHashtable['dataInPath']
foreach ($item in $inPath)
{
"Checking $item"
}
Extra tip: Import-PowerShellDataFile ensures that no rogue *.psd1 file can accidentally trigger unwelcome action. This command was added in PowerShell 5 as a function, so in Windows PowerShell you can view its source code:
This line copies the entire source code to your clipboard from where you can paste it into your PowerShell editor of choice. That’s complex but awesome code illustrating how you can verify PowerShell code structure using the internal parser, and much more.
In PowerShell 7, the command is now implemented as a binary cmdlet, so you can no longer view its source code this way.
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.
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.
Jan 16, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
Here is part two of our tips on PowerShell arrays.
Let’s explore some common caveats with arrays, starting with the...
Jan 7, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
PowerShell arrays are a powerful and versatile way of managing collections of data, enabling you to efficiently...
Dec 27, 2024 by Aleksandar Nikolić and Dr. Tobias Weltner
Tired of hidden errors in your PowerShell scripts? Discover how validation and transformation attributes can bring...
Tobias Weltner and Aleksandar Nikolić joinly wrote the blog post series 'Tobias&Aleksandar's PowerShell tips'. So we introduce both of them here:
----------------------------
Aleksandar Nikolić is a Microsoft Azure MVP and co-founder of PowerShellMagazine.com, the ultimate online source for PowerShell enthusiasts. With over 18 years of experience in system administration, he is a respected trainer and speaker who travels the globe to share his knowledge and skills on Azure, Entra, and PowerShell. He has spoken at IT events such as Microsoft Ignite, ESPC, NIC, CloudBrew, NTK, and PowerShell Conference Europe.
----------------------------
Tobias is a long-time Microsoft MVP and has been involved with the development of PowerShell since its early days. He invented the PowerShell IDE "ISESteroids", has written numerous books on PowerShell for Microsoft Press and O'Reilly, founded the PowerShell Conference EU (psconf.eu), and is currently contributing to the advancement of PowerShell as member in the "Microsoft Cmdlet Working Group". Tobias shares his expertise as a consultant in projects and as a trainer in in-house trainings for numerous companies and agencies across Europe.