Skip to the main content.

Unlocking the Power of PowerShell: Tips for Success

Why 'advanced functions' should be your PowerShell default

Did you know that simple functions could create hidden pitfalls? Advanced functions, created by adding [CmdletBinding()] and param(), ensure full PowerShell compatibility. These functions can utilize helpful features like $PSDefaultParameterValues, enhancing reliability and avoiding debugging issues.

 

 

Let advanced functions be your PowerShell default

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.

 

 

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

4 min read

Bulk Testing PowerShell Scripts with the Tokenizer

In Part 1, we explored the internal PowerShell parser to see how it analyzes code and breaks it down into individual...

3 min read

Mastering PowerShell Tokenization for Efficient Scripting

The internal PowerShell parser processes any code before execution. With full access to this parser, you can analyze...

3 min read

Using .NET Libraries in PowerShell - Functions, Cmdlets and .NET

In part 3, we identified a useful .NET method to display system dialogs and then wrapped it inside a new PowerShell...

About the author: