AutoRefactor Script

PowerShell scripts can be “dense” and almost unreadable at times. AutoRefactor automatically applies a series of best practices and turns a “wild” PowerShell script into a well-formed script that adheres to best practice rules.

This type of refactoring is done without actually executing the script and often is an efficient preparation to better understand, evaluate, and debug a script.

It also guarantees that your own scripts adhere to best practice standards.

Refactoring Script

To refactor a script, click the “Refactor Addon” button. This displays the refactoring tool.

Next, click “Fix Script Now”. The screen dims, and ISESteroids applies a series of best practice analyzer scripts.

When you click “Advanced”, you can see the individual script fixers. The currently active fixer script is highlighted.

Reorganizing Script

After the script is reorganized, all changes are marked with a green overlay. You can accept the changes, or press CTRL+Z do undo the changes.

Customizing Script Fixers

All script fixers listed in the tab “Advance” run in sequence when you refactor a script.

You can expand all categories, and enable or disable any fixer as you like. So if you do not like some fixing actions, simply disable them (or better yet, customize the way how they work until they do what you want. See below for more).

You can also drag&drop fixer scripts and categories to change the order in which they are applied. Use extreme care when you do this. Some script fixers depend on cleanup work done by others. For example, make sure you run a script fixer first that changes alias names to true command names. Other fixers may only act on commands and require alias names to be resolved first.

Running Individual Fixes

Click on the tab “Advanced”, and expand the category you need. Inside, you find a list of script fixers.

Click the fixer you want to run. ISESteroids applies the fixes, and reports back the number of changes made. You can accept or decline the changes.

For example, when pasting a script found in the Internet, often there are character changes such as changed hyphens, typographic quotes, or other illegal characters. To fix this, expand the category “Character Operations”, and click on “Illegal Characters”.

Reviewing and Adjusting Script Fixers

Advanced users can right-click any fixer script to review the code. You can even set breakpoints inside any fixer script, and once the fixer runs, review what the fixer script does, and how it is done.

This open architecture enables you to adjust script fixers exactly to your needs. You can add more script fixers, too.

“On Demand” Fixer Scripts

The script fixers listed in the tab “Advanced” are designed to run in sequence when refactoring a script. If you want to do individual special things, use the script fixers in the “On Demand” tab.

There is one sample fixer in this category. It replaces any literal path in a script by available environment variables to make a script portable.