Microsoft's Consistency in PowerShell CmdLet Parameter Naming

211 views Asked by At

Let's say I wrote a PowerShell script that includes this commmand:

Get-ChildItem -Recurse

But instead I wrote:

Get-ChildItem -Re

To save time. After some time passed and I upgraded my PowerShell version, Microsoft decided to add a parameter to Get-ChildItem called "-Return", that for example returns True or False depending if any items are found or not.

In that virtual scenario, do I have I to edit all my former scripts to ensure that the script will function as expected? I understand Microsoft's attempt to save my typing time, but this is my concern and therefore I will probably always try to write the complete parameter name.

Unless of course you know something I don't. Thank you for your insight!

1

There are 1 answers

8
Mathias R. Jessen On BEST ANSWER

This sounds more like a rant than a question, but to answer:

In that virtual scenario, do I have I to edit all my former scripts to ensure that the script will function as expected?

Yes!

You should always use the full parameter names in scripts (or any other snippet of reusable code).

Automatic resolution of partial parameter names, aliases and other shortcuts are great for convenience when using PowerShell interactively. It lets us fire up powershell.exe and do:

ls -re *.ps1|% FullName

when we want to find the path to all scripts in the profile. Great for exploration!

But if I were to incorporate that functionality into a script I would do:

Get-ChildItem -Path $Home -Filter *.ps1 -Recurse |Select-Object -ExpandProperty FullName

not just for the reasons you mentioned, but also for consistency and readability - if a colleague of mine comes along and maybe isn't familiar with the shortcuts I'm using, he'll still be able to discern the meaning and expected output from the pipeline.


Note: There are currently three open issues on GitHub to add warning rules for this in PSScriptAnalyzer - I'm sure the project maintainers would love a hand with this :-)