Skip to the main content.

PowerShell Tips

Always use advanced PowerShell functions

Table of contents

Post Featured Image

Tobias & Aleksandar's tip #7:

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

 

Overview of all 13 articles published during Scriptember


 

 

Always use advanced functions

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


Scriptember! Stay tuned:

During Scriptember, our calendar provides the best possible overview.  

Scriptember - hier ist die Übersicht

 

  • Follow us on social media, look for and use the hashtags #Scriptember and #PowerShell.
  • Join our live sessions, overview soon here.
  • Participate in challenges and contests. 
  • Share your knowledge.

 

Find all events here!

 

 

Related links 



Related posts

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...

4 min read

Hiding confirmations in PowerShell

Tobias & Aleksandar's tip #12:

The two PowerShell experts have teamed up to share their best and most helpful...

4 min read

URL Encoding in PowerShell

Tobias & Aleksandar's tip #11:

The two very well-known PowerShell experts have teamed up to share their best and most...

About the author: