Software Development optimize

Develop PowerShell code within Visual Studio

Former PowerShell skeptic Tony Patton describes how the PowerGUI Visual Studio Extension brings PowerShell development into Visual Studio.

IT professionals and administrators are the primary users of PowerShell, though developers are embracing the powerful tool as well. PowerShell's rudimentary command prompt interface leaves most users wanting more, especially developers who want to work within one tool. This is where the PowerGUI tool enters the picture as it provides a nice interface, and this is extended to Visual Studio with the PowerGUI Visual Studio Extension. This goal of this extension is to bring PowerShell development into Visual Studio.

The need for the PowerGUI Visual Studio Extension

Most UNIX aficionados roll their eyes when they hear about PowerShell, since UNIX has had powerful scripting tools since its inception. These tools make me think of command line interfaces, so using a Windows-based interface seems a bit counterintuitive. However, I spend a great deal of time within the Visual Studio IDE, so developing PowerShell within that environment is wonderful for me and most .NET developers.

The command line interface is nice, though a difficult aspect of learning and fully utilizing PowerShell is knowing what commands and their syntax are available. There are plenty of great books and online resources about PowerShell, but on-the-fly help is much more helpful. This is one of the features available with the PowerGUI Visual Studio Extension -- it provides IntelliSense support for PowerShell. Figure A shows this in action, as I get hints on what to type next while using the PowerShell editor within the Visual Studio 2010 IDE. Figure A

Using PowerShell IntelliSense to find the right command and syntax (Click the image to enlarge.)

Along with IntelliSense, the PowerGUI Visual Studio Extension provides the following features and more to make it easier to work with PowerShell.

  • PowerShell file and project types: You can create/edit PowerShell code files and assemble them into projects with more than one file.
  • PowerShell code snippets: The code snippet feature can be used for PowerShell code.
  • PowerShell console window: This feature provides the PowerShell console environment within Visual Studio IDE. This allows you to run commands or view output of scripts. Figure B shows the console window opened in the IDE.
  • PowerShell debugging: This feature is why I installed the extension; it provides a way to debug scripts within Visual Studio. It's a straight-forward way to locate syntax or logical problems in a script.
  • Syntax highlighting and script analysis: These are more Visual Studio features made available to PowerShell development.
Figure B

The PowerShell command window opened within the IDE. (Click the image to enlarge.)

Installation

In order to install the PowerGUI Visual Studio Extension, PowerGUI must be installed, with the caveat that the correct version of each product is installed; the website lists the necessary version of PowerGUI in the release notes. The current version of the PowerGUI Visual Studio Extension requires PowerGUI 3.2 is installed.

The PowerGUI application is installed as a standalone application; the PowerGUI Visual Studio Extension is installed as an extension, which can be managed via the Extension Manager window (Figure C). Figure C

The PowerGUI Visual Studio Extension shown in the Extension Manager window. (Click the image to enlarge.)

I encountered a few issues when installing the extension for the first time. I ended up uninstalling and reinstalling everything (i.e., PowerGUI and the Visual Studio Extension) to get it working properly. A quick look through the online discussions reveals I am not the only one who has run into trouble during installation, so you might refer to the online discussions if you experience problems.

Debugging

The ability to debug PowerShell scripts is still one of my favorite features of the PowerGUI Visual Studio Extension. Figure D shows a debugging session for a simple script. The yellow highlighted line is the current focus of execution. The console window shows output for previous steps in the code. The debugger is started with Shift-F5, and you can step through the code using the usual keyboard shortcuts (F10 and F11). Breakpoints are placed (and removed) in the leftmost column in the script pane (see the red dots in Figure D). The Call Stack and Watch panes provide a peek inside the script to further investigate issues. Figure D

Debugging a PowerShell script (Click the image to enlarge.)

Centralized development

I was skeptical about PowerShell when it was first introduced, but I have fallen in love with it over the past couple years. In addition to development, this powerful tool simplifies many administrative and support tasks. Developing PowerShell within Visual Studio allows me to stay in one place regardless of the type of code being developed.

PowerShell's tight integration with all current and upcoming versions of Windows makes me wonder if it will become a standard part of the Visual Studio IDE. I think it makes sense.

Are you a PowerShell fan or guru? Let us know by joining the discussion.

Keep your engineering skills up to date by signing up for TechRepublic's free Software Engineer newsletter, delivered each Tuesday.

About

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

1 comments
andy
andy

I don't suppose there's any chance of this extension working on Visual Studio Express is there?