More Basic Function Building
In the last article, I demonstrated how to add parameters to a basic PowerShell function. Well-crafter parameters make your code flexible, re-usable, and professional. If I see a function with a parameter of sys
and another using ComputerName
I think I know which one I’m more likely to use. The community accepted standards for writing PowerShell code aren’t arbitrary. These recommended best practices are intended to help you write better PowerShell. I have always stressed that you don’t need to be a developer to use or write PowerShell. But that doesn’t mean anything goes either. Common standards ensure consistency and predictability. But perhaps it will help if you know why we do certain things in PowerShell. That is one of the reasons for this newsletter, so let’s go back to building a PowerShell function.
Cmdletbinding
One of the easiest things you can do to make your code more professional is to insert the [cmdlebinding()]
attribute immediately after the function declaration.