Today, let’s check out how you can quickly add new functions to your scripts. ISESteroids has a number of code generators and wizards that can help you save time.
You can insert a blank new function body. There are a number of predefined templates (snippets) which you can adjust, and you can of course add more templates. To insert a new blank function body, first make sure the navigation bar is visible. Press ALT+N to toggle the bar. When visible, it is located at the top side of your script.
Next, right-click the “star” icon at the far left side of the navigation bar. This icon is enabled whenever your cursor is in a place where it makes sense to insert a function. A context menu opens and shows you a number of options:
- New Untitled Document: Opens a submenu with supported document types, and adds another script pane.
- Function Snippets: A list of function templates. When you hover over any of these, a tooltip window provides a code preview. You can also use the links in the code preview window. Click “Edit Snippet”, for example, to adjust the code.
- From Syntax: Opens a wizard that accepts a PowerShell function syntax. It then creates the function code from the syntax you entered. This is an excellent way of creating functions with a lot of parameters, or even multiple parameter sets.
- New Template: Opens the snippet editor and lets you create a new function snippet.
- Include Snippets with multiple definitions: when disabled, snippets that contain more than one function are excluded.
Selecting Default Snippet
To quickly insert a new function body, you do not necessarily need to open the context menu. Instead, simply click the icon in the navigation bar. This inserts the default snippet.
To choose the default snippet, open the context menu, hold CTRL, then click the snippet you want to insert. The snippet is now selected as default, and the next time you click the icon, this snippet will be inserted.
Creating Functions From Code
If you’d just like to enclose existing code in a function, select the code you want to enclose. Then, press CTRL+J to open the snippet selector.
The snippet selector now only shows snippets that can embed code. To quickly navigate to a snippet, enter part of the snippet name, for example “func”.
Or, you can select your code and then click the icon to the right side of the navigation bar. This brings up a wizard that helps you wrap your code into a function:
Make sure you pick a good name for your function. The wizard offers a list of approved verbs, so you just need to pick one, then add your function noun into the right textbox.
Note how the wizard automatically picks up all variables in your code. Make sure you check only those variables that you want to turn into parameters. Any variables you use internally need to be cleared.
Turning Static .NET Methods into Functions
There is yet another code generator available: if you found a useful static .NET method, simply right-click the method, then choose Refactor/Turn Method into Function.
This creates one PowerShell function per overload, so if you followed the example above and converted SetEnvironmentVariable to a PowerShell function, you end up with more than one PowerShell function, differing in the number of parameters they take. Pick the one that suits your needs.