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.
Unknown to many, PowerShell supports two different types of function: "simple functions" and "advanced functions".
Only "advanced functions" support all PowerShell features, and if there are no specific reasons why you explicitly want an old "simple function", as a safety rule, you should always ensure that all of your functions are "advanced functions".
The simplest way to ensure this is to add this to all of your functions:
[CmdletBinding()]
param()
If you want to define parameters, add them to the param() block. If you want to define special cmdlet features, add them to [CmdletBinding()]. For example, try this to add the -WhatIf and -Confirm parameters:
[CmdletBinding(SupportsShouldProcess)]
param()
From a user perspective, you can always verify that a function is indeed an "advanced function": It supports all the common parameters (i.e. -Verbose, -ErrorAction) whereas "simple functions" only support the parameters that you defined yourself.
Extra Tip: Simple functions lack many features that may not be evident at first. For example, they are not compatible with $PSDefaultParameterValues. Avoid simple functions so you don’t run in hard-to-debug edge case problems due to not supported internal functionality.
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.