Introduction
After you create a PowerShell snap-in, you probably want to create an installer. The "regular" way is to install from the command line using installutil.exe. But, it's not a good solution when you want to deploy your project.
In this article, you'll learn to create an MSI Setup Project (includes GUI) in Visual Studio and install your snap-in with 0 lines of code and without using installutil.exe at all.
The Solution
Your Visual Studio solution includes the project with the cmdlet classes and the snap-inT the output of this project is a DLL which is installed by installutil.exe.
Now, to create an installer, right-click in Solution Explorer on the solution name, and choose "Add New Project". Then, go to the Other Project Types category and choose Setup and Deployment -> Setup Project template.
The Install Project
The install project has a few parts. We will use only the File System part, the Custom Actions part, and the GUI. To switch between these views, right click on the project name in Solution Explorer (if you can't see it, go to the View menu and click on Solution Explorer), in the context menu, go to the View submenu, and click on the part you want to edit.
The first thing you have to do is go to File System (right-click on the project name in Solution Explorer and choose View->File System) and add the output of the main project.
In Application Folder, right-click and go to Add->Project Output.
Choose the project which includes the cmdlets (providers etc.) and the snap-in, and choose the Primary Output (the DLL, actually).
If you have any additional files, like Type Configuration files or Format Configuration files, add them.
Then, go to the Custom Actions view (right-click -> View->Custom Actions on project name in Solution Explorer), and add to the Install node a new custom action, by right-clicking and Add.
You will see something like this:
Choose your project output item from the application folder (you added it in the first step, in the File System view) and click OK.
Now, your Custom Actions view will look like this:
Now, do the same (add custom action, same as the previous step) in the Uninstall node. The Custom Action will look like this:
Right click on the project name in Solution Explorer and click Build. By default, the Installer doesn't build every time you build the solution. You can change it from the Configuration Manager in the Build menu.
After you build it, just right click and choose Install, or use the EXE and the MSI file which were created.
That's all!!!
This is a screenshot from the installer:
After the Installer runs, this is the output of get-pssnapin registered:
The installer automatically registers it. Now, your customer just has to use add-pssnapin from their PowerShell console to add it, or add it to their profile file.
If you have another application which uses the snap-in with Runspace, you can add the Custom Action to install the snap-in to the setup project of the host application (the application which uses the snap-in with Runspaces).
You can, of course, change the GUI, add steps etc.
The attached code sample
The attached code sample includes two projects:
- A snap-in project with a very simple cmdlet "Get-Proc" (in my previous article, you can read about how to create cmdlets).
- A setup project which installs the output DLL from the first project.