7 min read
Hidden configuration variables in PowerShell
Tobias & Aleksandar's tip #13:
The two very well-known PowerShell experts have teamed up to share their best and most...
PowerShell Tips
The two very well-known PowerShell experts have teamed up to share their best and most helpful PowerShell tips.
We will be publishing their scripts over the course of Scriptember in 13 blog posts. Don't miss their insights! Be sure to follow all Scriptember events, listed in our calendar here.
Class support was added to PowerShell in 2016 but is still not widely adopted – event though it can be very simple and very powerful.
Often scripts use simple numbers that can be cryptic, hard to understand, and error-prone:
# unstructured
$logLevel = 3
Enums are a perfect strategy to manage lists of values and provide friendly names to them:
# strictly-typed
enum MyLogLevel
{
No
Info
Verbose
Debug
All
}
$logLevel = [MyLogLevel]::Info
$logLevel
if ($logLevel -eq 'Info') { "INFO active" }
Internally, each enum is still a number, but user-friendly names help to understand the purpose, and the enum ensures that only valid numbers can be used:
Once you start using enums, this also improves usability. PowerShell automatically turns enums into IntelliSense menus. Here is a function called Write-LogMessage that illustrates this:
enum MyLogLevel
{
No
Info
Verbose
Debug
All
}
function Write-LogMessage
{
[CmdletBinding()]
param
(
[string][Parameter(Mandatory)]$Message,
[MyLogLevel]$logLevel = 'Info'
)
'{0:yyyyMMddHHmmss} {1,-7} {2}' -f (Get-Date), $logLevel, $Message
}
Part two of using enumerations in PowerShell is online, too. Click here to read tip #2!
During Scriptember, our calendar provides the best possible overview.
Oct 1, 2024 by Dr. Tobias Weltner and Aleksandar Nikolić
The two very well-known PowerShell experts have teamed up to share their best and most...
Oct 1, 2024 by Aleksandar Nikolić and Dr. Tobias Weltner
The two PowerShell experts have teamed up to share their best and most helpful...
Oct 1, 2024 by Dr. Tobias Weltner and Aleksandar Nikolić
The two very well-known PowerShell experts have teamed up to share their best and most...
Tobias Weltner and Aleksandar Nikolić joinly wrote the blog post series 'Tobias&Aleksandar's PowerShell tips'. So we introduce both of them here:
--------------------
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.
--------------------
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.