Easy Uninstall

From NSIS Wiki

For MUI 1.8/2.0(+), may work with no MUI.nsh but will need extra coding (Not supported)


This was simply created to make making an uninstaller easier and more convenient. It is equipped with shortcuts to every registry feature you can make for Windows' Add/Remove programs. Below will be examples of how to include it into your script. Just copy the sheet below (where designated) to a new empty file and place it in the directory "..\NSIS\Include\" as EasyUninstall.nsh (IT MUST HAVE THAT NAME! Location can vary, but you must use the '!AddIncludeDir "location\of\EasyUninstall.nsh"' before any commands for EasyUninstall are added!).


  1. Create a new text file in the folder "..\NSIS\Include"
    1. It must be named "EasyUninstall.nsh"
      1. (NO QUOTES!)
  2. Copy all the text in the box below into the file.
  3. Save it as "EasyUninstall.nsh" (as explained in Step 1.1)
/*									*\
	NSIS Generic Uninstaller 1.42
		;Scripted by AgnoMan Sen
	Works with NSIS 1.8-2.0(+)
\*									*/
 
!ifndef EASYUNINSTALLER_c0nf16x000001
!verbose Push
!verbose 3
	!macro EASYUNINSTALLER_funcM3x000001
		!ifndef EASYUNINSTALLER_c0nf16x000001
			!define EASYUNINSTALLER_c0nf16x000001
		!endif
		!ifndef EASYUNINSTALLER_c0nf16x000002
			!define EASYUNINSTALLER_c0nf16x000002
		!endif
		!ifdef EASYUNINSTALLER_c0nf16x000003
			!undef EASYUNINSTALLER_c0nf16x000003
		!endif
		!include EasyUninstall.nsh
	!macroend
	!macro EASYUNINSTALLER_funcM3x000002
		!ifndef EASYUNINSTALLER_c0nf16x000001
			!define EASYUNINSTALLER_c0nf16x000001
		!endif
		!ifdef EASYUNINSTALLER_c0nf16x000002
			!undef EASYUNINSTALLER_c0nf16x000002
		!endif
		!ifndef EASYUNINSTALLER_c0nf16x000003
			!define EASYUNINSTALLER_c0nf16x000003
		!endif
		!include EasyUninstall.nsh
	!macroend
 
	!define CONFIGUREUNINSTALL "!insertmacro EASYUNINSTALLER_funcM3x000001"
 
	!define IncludeUninstaller "!insertmacro EASYUNINSTALLER_funcM0x000000"
	!define IncludeUninstallerSection "!insertmacro EASYUNINSTALLER_funcM3x000002"
 
	!define UninstallerRootKey "!define _UninstallerRootKey"
	!define DONOTCONFIRMUNINSTALL "!define _DONOTCONFIRMUNINSTALL"
	!define DONOTINCLUDEUNINSTALLPAGE "!define _DONOTINCLUDEUNINSTALLPAGE"
 
	!define SILENTUNINSTALL "!define _SilentUninst"
	!define RemoveOnly "!define _RemoveOnly"
	!define ProductID "!define _ProductID"
	!define RegOwner "!define _RegOwner"
	!define RegCompany "!define _RegCompany"
	!define HelpLink "!define _HelpLink"
	!define HelpTelephone "!define _HelpTelephone"
	!define URLUpdateInfo "!define _URLUpdateInfo"
	!define URLInfoAbout "!define _URLInfoAbout"
	!define DisplayName "!define _DisplayName"
	!define DisplayIcon "!define _DisplayIcon"
	!define DpIconIndex "!define _DpIconIndex"
	!define DisplayVersion "!define _DisplayVersion"
	!define ModifyPath "!define _ModifyPath"
	!define VersionMajor "!define _VersionMajor"
	!define VersionMinor "!define _VersionMinor"
	!define EstimatedSize "!define _EstimatedSize"
	!define Comments "!define _Comments"
	!define InstallSource "!define _InstallSource"
	!define ParentKeyName "!define _ParentKeyName"
	!define ParentDisplayName "!define _ParentDisplayName"
!verbose Pop
!endif
 
!ifdef EASYUNINSTALLER_c0nf16x000002
 
	!ifdef MUI_INCLUDED
		!ifndef _DONOTCONFIRMUNINSTALL
			!insertmacro MUI_UNPAGE_CONFIRM
		!endif
		!ifndef _DONOTINCLUDEUNINSTALLPAGE		
			!insertmacro MUI_UNPAGE_INSTFILES
		!endif
	!endif
 
 
	!ifndef _RemoveOnly
		!define _RemoveOnly 1
	!endif
 
	!macro EASYUNINSTALLER_funcM0x000001
		!verbose Push
		!verbose 3
			!ifndef _UninstallerRootKey
				!define RR01_Root  HKLM
			!else
				!define RR01_Root ${_UninstallerRootKey}
			!endif
			!ifndef RR01_Key
				!define RR01_Key "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
			!endif
			!ifndef _ProductID
				!define RR01_Name "$(^Name)"
			!else
				!define RR01_Name "${_ProductID}"
			!endif
			!ifndef _DisplayName
				!define RR01_DisplayName "${RR01_Name}"
			!else
				!define RR01_DisplayName "${_DisplayName}"
			!endif
			!ifdef _DisplayIcon
				!ifdef _DpIconIndex
					!define RR01_DisplayIcon "${_DisplayIcon},${_DpIconIndex}"
				!else
					!define RR01_DisplayIcon "${_DisplayIcon}"
				!endif
			!endif
	!verbose Pop
	!macroend
 
	!macro EASYUNINSTALLER_funcM0x000002
		!ifdef RR01_Name
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "ProductID" "${RR01_Name}"
		!endif
		!ifdef _RegOwner
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "RegOwner" "${_RegOwner}"
		!endif
		!ifdef _RegCompany
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "RegCompany" "${_RegCompany}"
		!endif
		!ifdef _HelpLink
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "HelpLink" "${_HelpLink}"
		!endif
		!ifdef _HelpTelephone
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "HelpTelephone" "${_HelpTelephone}"
		!endif
		!ifdef RR01_DisplayName
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "DisplayName" "${RR01_DisplayName}"
		!endif
		!ifdef _URLUpdateInfo
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "URLUpdateInfo" "${_URLUpdateInfo}"
		!endif
		!ifdef _URLInfoAbout
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "URLInfoAbout" "${_URLInfoAbout}"
		!endif
		!ifndef _SilentUninst
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "UninstallString" "$\"$INSTDIR\uninstall.exe$\""
		!else
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "QuietUninstallString" "$\"$INSTDIR\uninstall.exe$\" /S"
		!endif
		!ifdef RR01_DisplayIcon
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "DisplayIcon" "${RR01_DisplayIcon}"
		!endif
		!ifdef _DisplayVersion
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "DisplayVersion" "${_DisplayVersion}"
		!endif
		!ifdef _RemoveOnly
			StrCmp ${_RemoveOnly} 1 +1 RR01RS7SK1
				WriteRegDWORD ${RR01_Root} "${RR01_Key}\${RR01_Name}" "NoModify" "0x00000001"
				WriteRegDWORD ${RR01_Root} "${RR01_Key}\${RR01_Name}" "NoRepair" "0x00000001"
				Goto RR01RS7
		RR01RS7SK1:
			StrCmp ${_RemoveOnly} 0 +1 RR01RS7
			!ifdef _ModifyPath
				WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "ModifyPath" "${_ModifyPath}"
				WriteRegDWORD ${RR01_Root} "${RR01_Key}\${RR01_Name}" "NoModify" "0x00000000"
				WriteRegDWORD ${RR01_Root} "${RR01_Key}\${RR01_Name}" "NoRepair" "0x00000001"
			!else
				WriteRegDWORD ${RR01_Root} "${RR01_Key}\${RR01_Name}" "NoModify" "0x00000000"
				WriteRegDWORD ${RR01_Root} "${RR01_Key}\${RR01_Name}" "NoRepair" "0x00000000"
			!endif
		RR01RS7:
		!endif
		!ifdef _VersionMajor
			WriteRegDWORD ${RR01_Root} "${RR01_Key}\${RR01_Name}" "VersionMajor" "${_VersionMajor}"
		!endif
		!ifdef _VersionMinor
			WriteRegDWORD ${RR01_Root} "${RR01_Key}\${RR01_Name}" "VersionMinor" "${_VersionMinor}"
		!endif
		!ifdef _EstimatedSize
			WriteRegDWORD ${RR01_Root} "${RR01_Key}\${RR01_Name}" "EstimatedSize" "${_EstimatedSize}"
		!endif
		!ifdef _Comments
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "Comments" "${_Comments}"
		!endif
		WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "InstallLocation" "$INSTDIR"
		!ifdef _InstallSource
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "InstallSource" "${_InstallSource}"
		!endif
		!ifdef _ParentKeyName
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "ParentKeyName" "${_ParentKeyName}"
		!endif
		!ifdef _ParentDisplayName
			WriteRegStr ${RR01_Root} "${RR01_Key}\${RR01_Name}" "ParentDisplayName" "${_ParentDisplayName}"
		!endif
	!macroend
 
	!macro EASYUNINSTALLER_funcM0x000000
		WriteUninstaller "$INSTDIR\uninstall.exe"
		!insertmacro EASYUNINSTALLER_funcM0x000001
		!insertmacro EASYUNINSTALLER_funcM0x000002
	!macroend
 
!endif
 
!ifdef EASYUNINSTALLER_c0nf16x000003
 
	Var RR01
	!macro EASYUNINSTALLER_funcM1x000001
		!verbose Push
		!verbose 4
			ReadRegStr $RR01 ${RR01_Root} "${RR01_Key}\${RR01_Name}" "InstallLocation"
				StrCmp $RR01 "A:\Program Files" RR01ER00 +1
				StrCmp $RR01 "B:\Program Files" RR01ER00 +1
				StrCmp $RR01 "C:\Program Files" RR01ER00 +1
				StrCmp $RR01 "D:\Program Files" RR01ER00 +1
				StrCmp $RR01 "E:\Program Files" RR01ER00 +1
				StrCmp $RR01 "F:\Program Files" RR01ER00 +1
				StrCmp $RR01 "G:\Program Files" RR01ER00 +1
				StrCmp $RR01 "H:\Program Files" RR01ER00 +1
				StrCmp $RR01 "I:\Program Files" RR01ER00 +1
				StrCmp $RR01 "J:\Program Files" RR01ER00 +1
				StrCmp $RR01 "K:\Program Files" RR01ER00 +1
				StrCmp $RR01 "L:\Program Files" RR01ER00 +1
				StrCmp $RR01 "M:\Program Files" RR01ER00 +1
				StrCmp $RR01 "N:\Program Files" RR01ER00 +1
				StrCmp $RR01 "O:\Program Files" RR01ER00 +1
				StrCmp $RR01 "P:\Program Files" RR01ER00 +1
				StrCmp $RR01 "Q:\Program Files" RR01ER00 +1
				StrCmp $RR01 "R:\Program Files" RR01ER00 +1
				StrCmp $RR01 "S:\Program Files" RR01ER00 +1
				StrCmp $RR01 "T:\Program Files" RR01ER00 +1
				StrCmp $RR01 "U:\Program Files" RR01ER00 +1
				StrCmp $RR01 "V:\Program Files" RR01ER00 +1
				StrCmp $RR01 "W:\Program Files" RR01ER00 +1
				StrCmp $RR01 "X:\Program Files" RR01ER00 +1
				StrCmp $RR01 "Y:\Program Files" RR01ER00 +1
				StrCmp $RR01 "Z:\Program Files" RR01ER00 RR01ER00SK
	RR01ER00:
		HideWindow
		MessageBox MB_OK|MB_ICONSTOP \
		"!Error: (RR01FNC0)$\n$\n$\tThe program has been installed to '$RR01' only!$\n$\tCan not remove, otherwise system will be corrupt.$\n$\tYou can still remove the program manually.$\n$\t$\t-Sorry for the inconvienence."
		Quit
	RR01ER00SK:
		!verbose Pop
	!macroend
 
	!macro EASYUNINSTALLER_funcM1x000002
		DeleteRegKey ${RR01_Root} "${RR01_Key}\${RR01_Name}"
	!macroend
 
	Function un.onUninstSuccess
		HideWindow
		MessageBox MB_OK \
		"Uninstallation complete.  Have a nice day!" \
		IDOK +1
	FunctionEnd
 
	Section Uninstall
		!insertmacro EASYUNINSTALLER_funcM1x000001
		Delete "$INSTDIR\uninstall.exe"
		!insertmacro EASYUNINSTALLER_funcM1x000002
		RMDir /r "$INSTDIR"
		RMDir /r "$INSTDIR"
		SetAutoClose true
	SectionEnd
 
!endif

(Once done, please see the next section for help on use of the file.)

Using EasyUninstall

Easy uninstall uses unique definitions, macro names and functions to attempt to not contradict other functions, however, this may occur. There are a few unique things about this you'll have to keep in memory.

  1. First off, when using MUI.nsh or MUI2.nsh, you must define the language after including EasyUninstall.nsh (and configuring it's settings.)
  2. All options are optional. If you do not use ${ProductID} [name] then it will default ${_ProductID} to $(^Name).
    1. However, ${CONFIGUREUNINSTALL} must be included always after !include EasyUninstall.nsh

Examples will be displayed below.

Examples

To see a preview of all EasyUninstall commands, please see http://nsis.sourceforge.net/Easy_Uninstall#Commands

Name "Default" ;This name will be used since there is no ${ProductID} command.
 
!include MUI.nsh
	!insertmacro MUI_PAGE_WELCOME
	!insertmacro MUI_PAGE_DIRECTORY
	!insertmacro MUI_PAGE_INSTFILES
	!insertmacro MUI_PAGE_FINISH
		/* Includes the MUI pages needed.  MUI_UNPAGE_* ARE NOT NEEDED! They will
		be automatically included, so please do not include them yourself!*/
 
!include EasyUninstall.nsh ;includes the EasyUninstall page (required)
	${CONFIGUREUNINSTALL} /*Tells EasyUninstall.nsh to configure itself.
				Explanation: When EasyUninstall.nsh is first
				ran as "!include EasyUninstall.nsh", it is
				designed to only define all the options in
				EasyUninstall.  However, since no options
				are included before ${CONFIGUREUNINSTALL}, it
				will cause all registry values and commands to
				use their default settings, and it will create
				the base functions to create an EasyUninstall
				feature.
					In Layman's terms, you need it there.*/
 
!insertmacro MUI_LANGUAGE "English" ;MUST BE INCLUDED AFTER THE PREVIOUS EASYUNINSTALL CODING
 
AutoCloseWindow true
 
InstallDir ""
ShowInstDetails hide
ShowUnInstDetails hide ;optional command to show the uninstall console
 
Section CreateUninstaller ;section name is irrelevant
	${IncludeUninstaller} ;Tells EasyUninstall.nsh to write the uninstaller.
SectionEnd
 
OutFile "TestFile.exe"

The above shows the very basic use for EasyUninstall.nsh You must still write an uninstaller section to tell the uninstaller.exe to remove certain strings/files/etc. However, EasyUninstall.nsh includes a pre-made section that will remove the entire $INSTDIR folder. However, this is not recommended by the NSIS team but there has been a safeguard installed. A message will appear if the program is installed to ?:\Program Files\ only and will cancel the uninstall. Inconveniently, the files will have to be removed manually. But there is no safeguard for "Documents and Settings" and other system folders (I may include those later), which means, DON'T INSTALL IT THERE! It will not require reboot but will require MUI.nsh or MUI2.nsh, and should remove everything in the folder and all registry strings (for the uninstaller only! If you have custom registry values, please create you're own uninstaller section! (you can still use ${IncludeUninstaller} and all of EasyUninstall's other functions.)

To include this section, please include the command ${IncludeUninstallerSection} by itself (that means in no section/function/macro) after everything else has been written (example below)

Section CreateUninstaller
	${IncludeUninstaller}
SectionEnd
 
${IncludeUninstallerSection}
 
OutFile "TestFile.exe"

When using EasyUninstall.nsh commands, they must be included after !include EasyUninstall.nsh and before ${CONFIGUREUNINSTALL}. EasyUninstall also allows you to exlude the MUI_UNPAGE_CONFIRM and MUI_UNPAGE_INSTFILES if you wish to make your own (see the commands below for more details.) An example will be displayed...

!include EasyUninstall.nsh
	${ProductID} "Name of your project"
	${DisplayName} "Name you want to see on Add/Remove Programs"
	${CONFIGUREUNINSTALL}
 
Section -Write
	${IncludeUninstaller}
SectionEnd
 
${IncludeUninstallerSection}

Commands

Below will be several commands that will write registry keys/values to help display the uninstaller on Window's Add/Remove Programs menu.

${UninstallerRootKey} [HKLM|HKCU]
/*Sets where the Uninstaller will be made in the registry.  Either HKLM (HKEY_LOCAL_MACHINE) or 
HKCU (HKEY_CURRENT_USER).  This will let you/your customer decide whether or not the uninstaller 
is made for all users of the computer or just the current user.  If you wish to make it relevant 
to the program itself, during install set their choice in a variable, and instead of HKLM or 
HKCU, use the variable in it's place.
This is defaultly HKLM, so if you wish to leave it that way, you don't need to use the command.*/
 
${DONOTCONFIRMUNINSTALL}
${DONOTINCLUDEUNINSTALLPAGE}
/*Excludes MUI_UNPAGE_CONFIRM and MUI_UNPAGE_INSTFILES from EasyUninstall in case you want to 
make you're own.  If you're using ${IncludeUninstallerSection}, DO NOT USE THESE COMMANDS!*/
 
${SILENTUNINSTALL}
/*Makes the uninstall "silent" when used from Add/Remove Programs menu. (untested)*/
 
${RemoveOnly} [0|1]
/*On Add/Remove Programs menu, there will be either the option to "Remove", "Change" "Remove", 
or "Change/Remove".  If set to 1 (default), it will only show "Remove". If set to 0, it will 
show "Change" "Remove" or "Change/Remove" depending on "${ModifyPath}"*/
 
${ProductID} [nameofproduct/registrykey]
;Registry key for the uninstaller (default=$(^Name))
 
${RegOwner} [name]
;Registered owner of the product (registry value)
 
${RegCompany} [name]
;Registered company of the product (registry value)
 
${HelpLink} [URL]
;Help link for the product (registry value)
 
${HelpTelephone} [number]
;Help number for the product (registry value)
 
${URLUpdateInfo} [URL]
;Update information for the product (registry value)
 
${URLInfoAbout} [URL]
;Information for the product (registry value)
 
${DisplayName} [name]
;Name displayed on the Add/Remove Programs menu (Default=${_ProductName})
 
${DisplayIcon} [directory\icon[,index]]
;Icon used in the Add/Remove Programs menu (registry value)
 
${DpIconIndex} [index] ;Do not use if you specified [,index] in ${DisplayIcon}
;Icon Index number for DisplayIcon (registry value)
 
${DisplayVersion} [#.#.#.#]
;Version shown for the product (registry value)
 
${ModifyPath} [directory\application]
/*Modify Path must be used with ${RemoveOnly} 1, and it will make "Change/Remove" into "Change"
"Remove" as well it will set "Change" to open an application to modify the program (untested)*/
 
${VersionMajor} [#x########]
;First number of the version number (registry DWORD)
 
${VersionMinor} [#x########]
;Last number(s) of the version number (registry DWORD)
 
${EstimatedSize} [#x########]
;Shows the Estimated Size of the product (in MB) (registry DWORD)
/*If you do not know what a DWORD is, please don't use these commands!*/
 
${Comments} [comment(s)]
;Comments about the product (registry value)
 
${InstallSource} [directory/source]
;Application which the product was installed from (registry value)
 
/*The following is only supported/tested with Windows XP Service Pack 2/3*/
${ParentKeyName} [registry\key\name]
/*If the product was an update for another product, please put in the registry key to help 
display the uninstaller on Add/Remove Programs
If it's "OperatingSystem", it's an update for Windows (registry value)*/
${ParentDisplayName} [name]
;Display name for the parent application (registry value)
 
${CONFIGUREUNINSTALL}
/*Configures the uninstaller with all the commands used (if any), if none are used, it will set 
the required ones to their defaults.  ${CONFIGUREUNINSTALL} is always required.*/
 
${IncludeUninstaller}
;Writes the Uninstaller into the script.  Must be included in a Section in the script!
 
${IncludeUninstallerSection}
/*Creates a generic uninstaller feature for the uninstaller to utilize (for simple installs)
Must be in a solo part of the script (not in a macro/function/section), preferably at the end
of the script!*/
Personal tools
donate