ISESteroids helps you write PowerShell code fast. You can use predefined reusable code snippets, auto-create function bodies, and automatically insert error handlers. PSSharper constantly checks your code for errors, best-practice suggestions, and detects issues like misspelled or undefined variables.
Aside from helping you to compose PowerShell code quickly, you’ll also constantly learn new things as you go.
Typing the same code statements over and over again takes time. ISESteroids supports real-time code snippet suggestions. When you type code, and the background of the current statement turns cyan, then you know there is a code snippet:
Stop typing, and press TAB instead. ISESteroids inserts the appropriate code snippet:
Right after you inserted the snippet, the status bar shows multiple clickable links to manage the snippet:
- “snippet content”: opens the Snippet Manager and lets you edit and change the snippet code
- “keyboard shortcut”: opens the Snippet Manager, too. Click “Edit Metadata” to change the snippet shortcut that triggers insertion
- “view all snippet shortcuts”: opens a GridView and lists all currently defined snippet shortcuts
Code snippet shortcuts work in the console pane, too. They are just not marked with a cyan background but if you know the keyboard shortcut and press TAB, snippets insert into the console just as they do in the editor pane.
Context Sensitivity and Multiple Edits
Note that snippet shortcuts may be context sensitive. When you enter “for” at the beginning of a line and press TAB to insert the snippet, a for loop is inserted. When you enter “for” following another command and press TAB, a foreach-object cmdlet is inserted.
Note also that some snippets support multiple edit. If you type “for” at the beginning of a line and press TAB, the for loop is not just inserted, but a number of fields are marked. You can continue to type to adjust the first field, then press TAB to move to the next field.
While in Snippet Input Mode, the status bar tells you what kind of adjustment can be made.
Some fields like the iterator variable have siblings, and when you change the variable, the variable changes in other places inside the snippet as well.
Click outside the snippet, or press ENTER, to end the snippet input mode.
Press CTRL+J to open the hierarchical snippet selector. It shows all available snippets, including those that have no keyboard shortcut.
When you hover over one of the snippets in the list, a tooltip displays the snippet code, and on the tooltip, you find clickable links to edit the snippet in the Snippet Editor. You can also create new folders in the snippet hierarchy, and new snippets. Click “New Folder” or “New Snippet” inside the selector.
Snippet Editor: Creating New Snippets
The snippet editor is a specialized editor designed to create advanced code snippets. You can open the editor for existing or new snippets. Let’s assume you often use collapsible regions and would like to create a snippet for this. You simply enter the code you want to turn into a snippet:
Next, select the code, then click the “hammer” icon in the left editor bar, and choose “Create Code Snippet from Selection”:
This opens the Snippet Editor and inserts the selected code. Continue to edit the code until it looks like you want it to be. Right-click the code, and choose “Set Desired Final Caret Position”. This will later be the final caret position after the snippet was inserted.
Creating AutoFill Fields
This step is optional. If you want the user to fill out elements of your snippet after it was inserted, select these portions of your code, then click the button “Field”.
This brings up another dialog where you can define the field:
- Supports SurroundWith: only one field can have this option enabled. When you later select code and then insert this snippet, the selected code will be inserted into this field. Do not use this for the region name. Instead, create a second field for the code “# Content”, and select “Supports SurroundWith” for that field. This will enable you to surround selected code with your code region.
- Show in Selection menu: when “Supports SurroundWith” is enabled, and you enable this option, then your snippet will show in the “Hammer” menu that appears in the left editor bar once you select code.
Click OK when you are done. Fields are marked with a thin line.
Your region has two identical texts “MyRegion”: one at the beginning, and one at the end. Click the button “Include Siblings” to auto-apply field content to any other portion of the code that has the same content.
This is what the Snippet Editor may look like after some editing:
- Defines the final caret position. Set or change by right-clicking into the editor.
- Defines an Auto-Fill field. Edit to right-click into the field.
- Defines a sibling. Automatically marked when you click into the parent field and the button “Include Siblings” is enabled.
- Defines another Auto-Fill field. This field should have the options “Supports SurroundWith” and “Show in Selection menu” enabled so that later selected code is inserted here.
When you are done, click OK. A final dialog opens and asks for some metadata. Enter at least a title. It is recommended that you also add a keyboard shortcut. Choose a keyboard shortcut that you would typically enter anyway, like “#region” in this case:
Once you click OK, you are asked where to save the snippet, and the snippet is created. The default location for user defined snippets is the folder “Snippets” inside your “WindowsPowerShell” folder.
Using Your New Snippet
You can immediately use your new snippet!
When you enter your keyboard shortcut, its background turns cyan, and you can insert your snippet by pressing TAB. Since you defined auto-fill fields, the snippet enables input mode, and you can customize it.
Note how setting the start tag also sets the end tag because you defined sibling fields.
Next, select some existing code. Then click on the Hammer icon in the left editor margin, and choose “Surround With”. Your snippet is listed, and when you choose it, your selected code is surrounded by a new collapsible region. All you need to do is set the region name, and conveniently enough the end tag is again automatically set, too.
Snippets created by ISESteroids are compatible with the native ISE. In the native ISE, you can also press CTRL+J to open a snippet selector and insert your newly created snippets. The snippet selector in native ISE is one-dimensional, there are no snippet keyboard shortcuts, and no auto-fill fields, though.
If you’d like to rename variables, or other items in your script, simply click the item (do not select any text), then press F2. All instances of the same kind are selected, and when you type, all related instances are renamed as well.
- Click variable to rename, press F2, enter a new name
- All related variables are also selected and renamed
- All affected parameters are also selected and renamed
Selecting Text Does Simple Search&Replace
When you select text (rather than just clicking on some element), F2 performs a plain text search&replace. It then selects all text that matches your selected text.
While plain text search&replace can be useful, never use it to rename variables. As you have seen, renaming a variable has effects on parameters and other elements that a plain text search&replace would not find.
Once you select code in ISESteroids, a “Hammer” icon appears in the left editor margin. Click it to view refactoring options.
The list of refactoring options depends on the code you selected but would typically look similar to this:
- Surround with: shows a list of code snippets that support this feature. The snippet will be inserted, and the selected code is placed inside the snippet.
- Comment out: Selected code is commented out (CTRL+SHIFT+B)
- Align assignment operators: if selection contains at least two assignments, the assignment operators are aligned
- Copy as HTML: Selection is converted to HTML and placed as text onto the clipboard. HTML can be used to display the selected code in high fidelity in blog posts and on websites
- Convert Syntax to Function: if the selection resembles a valid syntax, defining a function with its parameters, then this will convert the syntax to the full PowerShell function code
- Create Code Snippet from Selection: turns the selected code into a reusable code snippet (see section “Advanced Code Snippets”)
- Auto-Create Function: takes the selected code, and wraps it into a function body. Variable assignments can be turned into function parameters.
- Expand selection: selects the next higher PowerShell language structure (CTRL+Q)
PSSharper is a highly efficient code analysis engine working in the background as you code. It adds warnings and suggestions that show up in the PSSharper Bar at the bottom of the editor as well as through adornments and tooltips right in your code.
- A ghosted variable, indicating that this variable is defined but never used anywhere
- A strike-through variable indicating that this assignment is invalid because the variable name is reserved by PowerShell
- A light bulb indicating auto-refactor options to fix the issue. Click the light-bulb to see options
- A textmarker background indicating that the variable is undefined and has no value
- A tooltip explaining the issue in more depth. Appears when you hover over an issue.
- A thin dotted green underline, indicating that there are refactoring options. Click the adornment to see a light-bulb in the editor margin
- A “close” button to slide the PSSharper Bar out of view. You get it back via menu “PSSharper / Show PSSharper Bar”
- PSSharper Bar reports the type and number of issues found. Click a button to open the PSSharper Addon with more details.
Set the Target PowerShell Version
One of the first things you ought to do is set the PowerShell version(s) you want to target. Many issues are version related, and many auto-fixes are applicable to certain PowerShell versions as well. So in order to not receive unnecessary issue warnings, choose “Compatibility / Set PowerShell Target”. A dialog opens. Set the desired target versions, and click OK.
When you click an issue in your code, PSSharper frequently shows a yellow or red light bulb in the left editor margin, indicating there are automatic refactoring fixes available.
- There may be multiple fixable issues
- All fixes are just suggestions. Apply fixes only if they make sense to you. You can always undo changes by pressing CTRL+Z.
- Manage Issue: opens the PSSharper Addon, selects the issue in the issue list, and offers additional options (see below)
When you press CTRL+ENTER, PSSharper sets your caret to the next issue. If the caret is already on an issue, and there is a default auto-fix, CTRL+ENTER invokes it.
PSSharper AddOn Bar
When you click an issue group on the PSSharper Bar, or when you choose “Manage Issue” in the light bulb context menu of a particular issue, the PSSharper Addon opens. It lists all issues of a given category:
- Currently selected targeted PowerShell versions. Click to change.
- Load and Save PSSharper configurations.
- Category Name of currently listed issues
- Rule, listing all issues of the same kind
- Rule Personalization: you can choose to ignore a particular issue type, or use different adornments
- Visual issue preview
- Click the magnifying icon to open a menu with options
- Fixes the currently selected issue (if automatic fix is available)
- Fixes all batch-enabled rules in entire script. Click on button #5 to batch-enable a particular rule.
- Exports all issues to a CSV file for external analysis and reportin (you can also run Get-PSSharperData)
ISESteroids can apply a set of best practice formatting guidelines to a script. Often, when you receive code from other sources, this is a good start to formalize the script code so it becomes easier to read. Click the button for the Auto-Format Addon. Next, click “Fix Script Now”.
This will start auto-formatting the current script and may take a couple of seconds. In the console pane, the formatter reports the action it takes.
If you need more control over what gets formatted, or want to invoke only individual fixer units, click the tab “Advanced”:
- Click the Auto-Format Button to show or hide the Auto-Format Addon.
- Click tab “Advanced” to view the individual fixer units that perform auto formatting
- Click a category to see the individual files
- Right-click a fixing unit to open the underlying PowerShell code, or the description file, or the file location.
- Click “Apply” to run this fixer unit only.
ISESteroids can create function bodys for you in a number of convenient ways:
Syntax to Function
If you are familiar with command syntax description, then you can start to design a new function by defining its syntax. Simply enter the desired syntax into the editor, and do not worry about error messages from PowerShell (since you are not entering valid PowerShell code).
Let’s take for example a syntax like this:
The syntax definition defines function name, parameter names (prefix “-“) and argument data types (enclosed in “<>”). It also marks optional parts in brackets. So this example reads:
- The function name should be “Add-Customer”
- The first parameter should be -Name of type “String” (Text), and the parameter should be mandatory and positional (you can omit the parameter, but not the argument)
- The second parameter is completely optional, but you can also specify it positional (just the argument)
- The third parameter is optional and not positional. If you use it, you must specify the parameter name and the argument.
- The forth parameter is a Switch parameter. It has no value.
Select the syntax definition. In the left editor margin, a “Hammer” icon appears. Click it, and choose “Convert Syntax to Function” (this command is disabled when the selected syntax definition is invalid).
ISESteroids creates the function code that matches your syntax definition:
You can even define multiple parameter sets by selecting more than one syntax definition. Here is an example:
When you select both lines and convert them to function, the code looks like this:
And when you run the code, you will see that the newly created function “Connect-Server” behaves like intended: you must specify -id, you can specify -ComputerName, but if you do specify -Credential, then -ComputerName becomes mandatory:
Code to Function
Here is some code that you might have created after some playing around with PowerShell:
Now you would like to turn it into a function. Simply select the code, then right-click the selection, and in the context menu choose “Selection / Auto-Create Function”. A dialog opens:
The dialog lets you pick a function name. Carefully review the variables detected in the “Parameter” section. Check only those that you want to turn into parameters. Do not check variables that serve internal purposes and should not be exposed to the user. Then, click OK. The code is generated for you:
Cmdlet to Function
If you’d like to find out how a particular cmdlet defined its parameters so you can mimick this in own functions, have ISESteroids create a function body from cmdlet. To do this, enter the cmdlet name that you want to mimick, for example “Get-Random”.
Then, right-click it and choose “Source Code / Convert to Function”. This is the resulting code:
You can immediately see how Get-Random defined its parameters, and learn from it. You may discover new Validate attributes and find out about parameter Aliases.
.NET Method to Function
Static .NET methods can be turned into true PowerShell functions with just a click. Here is a static .NET method that can set an environment string:
A raw .NET call does not tell the user what arguments are required, has no error handling, is not easily reusable. To turn the .NET method SetEnvironmentVariable into a PowerShell function, right-click the method and choose “Refactor / Turn method into function”. Here is the resulting PowerShell code:
Note that ISESteroids may create a number of functions, one for each overload. Pick just the one you want to keep, and delete the rest. Once the .NET static method is turned into a PowerShell function, it is very easy to use and can be exported to modules. Here you can see how easy it is now to set environment variables with your new function:
You can compose new functions with snippets. When you enter “func”, the keyword gets a cyan background, and when you now press TAB, a function body is inserted, and you can assign it a name, then press ENTER.
The caret is moved inside the function. Next, enter “par”. Again, the keyword gets a cyan background indicating that there is a snippet available. Press TAB to insert a param() block, choose a name for the first parameter, and press TAB. This moves focus to the second parameter. Assign it a name, too, and press ENTER.
You can create more snippets (see “Advanced Code Snippets”) to tailor function creation exactly to your needs.
When your script encounters an unhandled runtime exception, PowerShell typically emits one of its red error messages:
Using Simple Error Messages
You can right-click the console and enable “Simple Error Messages” to reduce the clutter and only show the essential information, which reduces the exception message like this:
Shortening error messages works only for terminating errors. It does not change the error message emitted from cmdlet that use -ErrorAction Stop.
If the error was caused by some command in your script, ISESteroids marks it with a red squiggle. You can hover over it to find out more about the exception:
Adding Error Hander Code
If you want to respond to the error, you need to add an error handler. ISESteroids takes out the complexity and adds an appropriate handler for you.
In the left editor margin, a red light-bulb indicates there are auto-fixes available. Click the bulb, and choose “Add specific error handler”. ISESteroids automatically inserts an error handler that is able to catch this exception. If necessary, it also adds the -ErrorAction parameter to the cmdlet call. All edits are marked with a green background that is removed as soon as you click or edit the script:
When you take a closer look at the inserted error handler, you’ll discover that ISESteroids automatically determined the exception type and added a specific catch clause for this exception type. Any other error will still show up. If other errors cause runtime exceptions, repeat the steps above to add more specific catch clauses.
Note also that ISESteroids stores the error record in $_ into a strongly-typed variable $e. This way, when using $e you get full IntelliSense.
Simplifying Error Handler
The inserted error handler is just an initial example. You can always take the information gathered in $info, and write it to some log file, or simplify and change the error handler as you like. Just tailor it to your needs: