Here’s how you can get a sweet Visual Studio development experience for building and debugging your own PowerShell cmdlet:
– It has Wizard support for initially creating the cmdlet,
– F5 build support which also registers your cmdlet
– provides the full power of the VS debugger including Edit-and-Continue when debugging your cmdlets.
David Aikens has a great tutorial for how to build a Power Shell cmdlet in the VS ide. This does 99% of the work. He’s got great wizards that makes things very easy to start. His tutorial describes the rest of the steps that need to be done manually. Conveniently, all of these steps can be automated. I’m assuming there’s already some sample VS project in the Power Shell SDK or somewhere that does that; but if you want to create such a project by hand, here’s how…
- Install David’s wizard from https://channel9.msdn.com/Photos/ZippedFiles/256835_PSTemplates.zip. This provides a great integrated wizard in ‘New Project’ / ‘Add New Item’ which has a starting implementation of a cmdlet.
- Add the installutil.exe command as a post-build step:
This will register the snapin in a global list in the registry such that PowerShell can find it. After this command, the snap-in should show up with the ‘get-pssnapin -registered‘ command in powershell.
- In the debug settings under Project Properties, for “Start Action”, use “Start External Program”, and specify Power shell:
Eg, something like:
The Start Action controls what happens when you press ‘F5’. If you’re building an exe, usually you want it to just launch that exe. In our case, we’re building a dll and we want it to PowerShell and have that load our dll.
- For command line arguments, specify:
-noexit -command add-pssnapin test
Where ‘test‘ is the name of your snap-in that you registered (PSSnapIn.Name property in PsSnapin.cs). These specify the command line arguments to the exe we’re launching. In our case, we’re launching PowerShell, and we want PowerShell to automatically load our cmdlet. The -command parameter to PowerShell tells it to execute the ‘add-pssnapin test’ command at startup, which loads the cmdlet that you’re building in the IDE. The -noexit switch tells PowerShell to not exit after doing -command.
The Debug settings should look like:
- Try it out! Also see http://msdn2.microsoft.com/en-us/library/ms714598.aspx for more tips on writing a cmdlet, such as adding parameters.
Now you can develop your cmdlet in the IDE as you’d expect, complete with intellisense. When you Build in the IDE, it will automatically run installutil and install your cmdlet. When you F5, it will launch PowerShell under the debugger, and load your snap-in. Thus you can use the full power of the Visual Studio debugger on your cmdlet. When you execute your cmdlet, you’ll hit breakpoints, etc.
You can even use edit-and-continue on your cmdlet. (Unfortunately, EnC doesn’t support adding public properties, so you can’t add new properties to your cmdlet).
You can also enable Just-my-code which will treat your cmdlet as user-code and Power Shell as non-user code and make it easier to focus on debugging just your cmdlet without debugging Power Shell.