About Me

My photo
I'm a colonist who has declared war on machines and intend to conquer them some day. You'll often find me deep in the trenches fighting off bugs and ugly defects in code. When I'm not tappity-tapping at my WMD (also, known as keyboard), you'll find me chatting with friends, reading comics or playing a PC game.

Thursday, February 12, 2009

Creating an uninstall shortcut for your MSI package

Microsoft, as a general rule, expect every user to uninstall applications from the Add/Remove Programs tool in the Control Panel. However, there might be cases where one might want to add an 'Uninstall Program' shortcut to the Start->Programs Menu. The steps below are a walkthrough for creating a simple MSI package using Visual Studio 2008 and adding an 'Uninstall Program' shortcut to the Programs Menu as well.

Basic Walkthrough for creating an MSI Installation package in Visual Studio 2008.
  1. Open the project for which an MSI Installation package is to be added in Visual Studio 2008.
  2. Right click the Solution of the Project in the Solution Explorer and select Add->New Project from the context menu.
  3. Expand the 'Other Project Types' category and choose 'Setup Project' from the 'Setup and Deployment category'. Enter a name and location for the MSI package to be created and click OK.
  4. A Setup project will be added to the project solution. Right Click the Setup Project in the Solution Explorer and select View->File System from the context menu.
  5. Right click 'Application Folder' in the new window and select Add->Project Output. Now, select 'Primary Output' from the dialog box that pops up and click OK. A 'Primary Output from xxx' entry should appear in the Application Folder. This is the main executable of the project.
  6. Right click the 'Primary Output from xxx' entry and select 'Create Shortcut to Primary Output from xxx'. Repeat this step one more time to create two shortcuts.
  7. Cut one of the shortcuts and paste it in the User's Desktop folder. Similarly cut the other shortcut and paste it in the User's Program Menu folder. Rename each of these shortcuts to something more appropriate (such as the application name).
Creating an Uninstall Program shortcut.
  1. Browse to the MSI project folder (using Windows Explorer), right click and select New->Shortcut from the context menu. In the Create Shortcut Wizard dialog that pops up type '%windir%\system32\msiexec.exe -x {prodCode} ' as the location of the shortcut, where prodCode is the Product Code of the MSI package. This Product Code can be identified from the Project Properties of the MSI Project in Visual Studio. Also, provide a proper name for the shortcut (such as Uninstall xxx, where xxx is the name of the application) and click Finish.
  2. The next step involves adding this shortcut to the User's Programs Menu folder of the MSI project in Visual Studio. The problem is that files with extension .lnk (extension of the shortcut) cannot be added to the Project. So, first we need to rename the shortcut extension from .lnk to .txt. Open up a DOS command window and browse to the location of the shortcut using the 'cd' command. Now type 'ren xxx.lnk xxx.txt' where xxx is the name of the shortcut item.
  3. Now, simply drag the renamed shortcut into the User's Programs Menu folder of the MSI project.
  4. Rename the shortcut from .txt back to .lnk.
  5. Build the MSI project and the necessary setup files will be created in the bin folder of the project.
Hope this helps someone out. Have a good day.


itzkakarot said...

Thanks a lot, works like a charm. In your instructions, just include one more step to rename 'xxx.txt' back to 'xxx.lnk' within Visual Studio before building the MSI.

Also, I was wondering is there any way we could do this in some generic way from within Visual Studio itself? Would be great if we could somehow add a custom generic shortcut that pulls the [ProductCode] property within the installer, this way you wouldn't need to manually create the Uninstall shortcut for each product. I couldn't find any way to do this, didn't know if you might know of any other tricks.

Thanks again!

Angelo said...

Thanks for reminding me to add the step for renaming the shortcut from .txt back to .lnk in the MSI Project. I'm afraid, I don't know of any generic way by which you can create the Uninstall Program shortcut for multiple MSI projects. Microsoft makes it pretty hard for us to this

Jignesh said...

Thanks. This is exactly what I was looking for. There are other ways to do it using batch file but I liked this way.

wvd_vegt said...

Thanks a lot for this guide (the uninstaller was bugging me). Perhaps you could also add where the ProductCode can be found:

View the Properties Window when the installer project is selected in the Solution Explorer (and not the Properties menuitem in its context menu, the mistake I made).

Maurizio said...

Hi, this does not work in Vista and Windows 7. When you click the uninstall link the Start Menu, it says "this option is only available for installed products". I have specified the correct product code though.

Could it have something to do with UAC? Cheers

Angelo said...

I'm afraid I don't have access to a Windows 7 or Vista machine...I will try this out on a friend's PC and post the result.

Dorababu said...

Can i get more clear from the second step i did not find any .lnk file so can you please tell what to do

Angelo said...

Hi Dorababu,

lnk is the extension of the shortcut that you create in the first step. Were you able to complete the first step successfully?

Dorababu said...

Ya i completed my first step successfully but the later i am not getting

Dorababu said...

Angelo can you tell me what to do form the 2nd process..

Dorababu said...

At-last i got the required one. Thanks for the article.

Angelo said...

Hi Dorababu,

Sorry I couldn't reply earlier. Its great to know that you figured it out. Cheers!!

Victory said...

Thank you so much... You saved me.... Thank you Thank you... :)