1

In powershell you can declare parmeters like:

function My-Function($someParemeter) { Do something... }

But can also use:

function My-Function { param($someParameter) DoSomething... }

What is the difference? When should I use one or the other?

mklement0
  • 382,024
  • 64
  • 607
  • 775
Daniel Oliveira
  • 8,053
  • 11
  • 40
  • 76
  • There is none that I'm aware of expect for using a `param` block is a lot cleaner – Santiago Squarzon Nov 03 '22 at 19:50
  • 2
    You can specify [`Cmdletbinding()`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_cmdletbindingattribute?view=powershell-7.2) attribute only when using `param`. Note that you can still create an advanced function cmdlet even without `param`, by using `[Parameter()]` attribute in front of parameter name. – zett42 Nov 03 '22 at 20:05
  • @zett42 I think thats worth an answer, totally forgot cmdletbinding was not possible using the first syntax clearly never use it :P – Santiago Squarzon Nov 03 '22 at 20:19
  • 2
    @Santiago, we already have a post that covers this - please see the linked duplicate. Daniel, I hope that duplicate answers your question. – mklement0 Nov 03 '22 at 20:24
  • Amazing answer and my question is definitely a duplicated. Thanks guys :) – Daniel Oliveira Nov 04 '22 at 20:39

0 Answers0